From 4a01081e83b1092a426c2ded19b02f56db46effd Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 19 Nov 2021 14:58:00 +0400 Subject: [PATCH] Thread-safe Book::Illustration::getData() --- include/book.h | 2 ++ src/book.cpp | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/book.h b/include/book.h index 5d049ad6c..cdcbf51fe 100644 --- a/include/book.h +++ b/include/book.h @@ -23,6 +23,7 @@ #include #include #include +#include namespace pugi { class xml_node; @@ -57,6 +58,7 @@ class Book private: mutable std::string data; + mutable std::mutex mutex; }; typedef std::vector> Illustrations; diff --git a/src/book.cpp b/src/book.cpp index bf7c3e18f..63ebbec19 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -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 l(mutex); + if ( data.empty() ) { + try { + data = download(url); + } catch(...) { + std::cerr << "Cannot download favicon from " << url; + } } } return data;