Thread-safe Book::Illustration::getData()

This commit is contained in:
Veloman Yunkan 2021-11-19 14:58:00 +04:00
parent eb6a0d6456
commit 4a01081e83
2 changed files with 9 additions and 4 deletions

View File

@ -23,6 +23,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <mutex>
namespace pugi { namespace pugi {
class xml_node; class xml_node;
@ -57,6 +58,7 @@ class Book
private: private:
mutable std::string data; mutable std::string data;
mutable std::mutex mutex;
}; };
typedef std::vector<std::shared_ptr<const Illustration>> Illustrations; typedef std::vector<std::shared_ptr<const Illustration>> Illustrations;

View File

@ -227,12 +227,15 @@ const Book::Illustration& Book::getDefaultIllustration() const
const std::string& Book::Illustration::getData() const const std::string& Book::Illustration::getData() const
{ {
if (data.empty() && !url.empty()) { if (data.empty() && !url.empty()) {
const std::lock_guard<std::mutex> l(mutex);
if ( data.empty() ) {
try { try {
data = download(url); data = download(url);
} catch(...) { } catch(...) {
std::cerr << "Cannot download favicon from " << url; std::cerr << "Cannot download favicon from " << url;
} }
} }
}
return data; return data;
} }