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,10 +227,13 @@ 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()) {
try { const std::lock_guard<std::mutex> l(mutex);
data = download(url); if ( data.empty() ) {
} catch(...) { try {
std::cerr << "Cannot download favicon from " << url; data = download(url);
} catch(...) {
std::cerr << "Cannot download favicon from " << url;
}
} }
} }
return data; return data;