From 28b8fca7298b7de8caf625837c0d6269fbe6303e Mon Sep 17 00:00:00 2001 From: Igor Kushnir Date: Sun, 9 Jun 2019 13:53:47 +0300 Subject: [PATCH] Reader: make PageLoader::working atomic to fix a data race This data member is modified in PageLoader's own thread and accessed without locking from an external thread in the public busy() function. Reorder setting working and img in PageLoader::run() to avoid a data race in PageLoader::result() called from GoToFlow::updateImageData(). --- YACReader/goto_flow.cpp | 4 ++-- YACReader/goto_flow.h | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/YACReader/goto_flow.cpp b/YACReader/goto_flow.cpp index 27c0c000..110a4aa1 100644 --- a/YACReader/goto_flow.cpp +++ b/YACReader/goto_flow.cpp @@ -244,7 +244,7 @@ PageLoader::~PageLoader() bool PageLoader::busy() const { - return isRunning() ? working : false; + return isRunning() ? working.load() : false; } void PageLoader::generate(int index, QSize size, const QByteArray &rImage) @@ -283,8 +283,8 @@ void PageLoader::run() mutex->unlock(); mutex->lock(); - this->working = false; this->img = image; + this->working = false; mutex->unlock(); // put to sleep diff --git a/YACReader/goto_flow.h b/YACReader/goto_flow.h index d119e8b5..09fe2ce0 100644 --- a/YACReader/goto_flow.h +++ b/YACReader/goto_flow.h @@ -9,6 +9,8 @@ #include #include +#include + class QLineEdit; class QPushButton; class QPixmap; @@ -89,7 +91,7 @@ private: QWaitCondition condition; bool restart; - bool working; + std::atomic working; int idx; QSize size;