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 <vector>
#include <memory>
#include <mutex>
namespace pugi {
class xml_node;
@ -57,6 +58,7 @@ class Book
private:
mutable std::string data;
mutable std::mutex mutex;
};
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
{
if (data.empty() && !url.empty()) {
try {
data = download(url);
} catch(...) {
std::cerr << "Cannot download favicon from " << url;
const std::lock_guard<std::mutex> l(mutex);
if ( data.empty() ) {
try {
data = download(url);
} catch(...) {
std::cerr << "Cannot download favicon from " << url;
}
}
}
return data;