mirror of
https://github.com/YACReader/yacreader
synced 2025-05-27 10:50:27 -04:00
In a later commit WorkerThread should also replace classes similar to ImageLoader: PageLoader, ImageLoaderGL and ImageLoaderByteArrayGL. Bugs fixed: 1. Eliminated a data race between ImageLoader::run() and ComicFlow::updateImageData()->ImageLoader::result(). Specifically when ImageLoader::busy() returns false, then ImageLoader::run() sets ImageLoader::working to true, loads the image and starts assigning it to ImageLoader::img, while ImageLoader::result() is accessed without locking from updateImageData(). Making ImageLoader::working atomic is clearly insufficient to eliminate this data race. The fix is to set 'working' to true immediately and synchronously as soon as a new task is assigned to the worker. 2. Replaced thread termination with graceful thread exit. ComicFlow destructor called QThread::terminate(), using which is discouraged by Qt documentation. The application exited without errors in Release mode. In Debug mode, however, it received the SIG32 signal on exit and printed the following warning - "QWaitCondition: mutex destroy failure: Device or resource busy". The loop in WorkerThread::run() is no longer endless. The worker thread properly ends and is joined in WorkerThread destructor. Design decisions: 1. WorkerThread could emit a signal when it completes a task. Thus updateTimer could be removed from ComicFlow and GoToFlow. However, there is no obvious way to use this new signal in the two GL classes. Also I don't know whether updateTimer is just an inefficient polling substitute for notification or an intentional animation mechanism. 2. The index variable is no longer stored in the worker class, but in ComicFlow directly. Thing is, this data member was never actually accessed by the worker, but ComicFlow went so far as to lock worker's mutex to "protect" access to the index. 3. The common ImageLoader implementation turned out to be very general. So I converted it into the WorkerThread class template that is not restricted to producing QImage results and can be reused elsewhere. 4. I used standard classes (such as std::thread) instead of their Qt equivalents (e.g. QThread) because they are more thoroughly documented. The standard classes should also be more efficient as they were more carefully designed and provide much fewer unnecessary features. 5. Release-Acquire ordering is safe for the WorkerThread::working use case and is more efficient than the std::atomic-default Sequentially-consistent ordering. 6. condition.notify_one() is called while the mutex is unlocked to improve performance. This is safe in both cases: a) if the worker thread exits due to a spurious wakeup just before the condition.notify_one() call in WorkerThread destructor, so much the better; b) if a spurious wakeup lets the worker thread finish the task and start waiting on the condition again just before the condition.notify_one() call in WorkerThread::performTask(), the second waking will be ignored by the worker thread as 'working' and 'abort' will be false then.
92 lines
2.0 KiB
C++
92 lines
2.0 KiB
C++
#ifndef WORKER_THREAD_H
|
|
#define WORKER_THREAD_H
|
|
|
|
#include "release_acquire_atomic.h"
|
|
|
|
#include <cassert>
|
|
#include <utility>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
#include <thread>
|
|
|
|
//! Usage:
|
|
//! 1. call performTask();
|
|
//! 2. wait until busy() returns false;
|
|
//! 3. (optionally) call extractResult();
|
|
//! 4. return to step 1 (assign another task).
|
|
//! You may invoke busy() and the destructor at any moment.
|
|
template<typename Result>
|
|
class WorkerThread
|
|
{
|
|
public:
|
|
WorkerThread() = default;
|
|
~WorkerThread();
|
|
|
|
using Task = std::function<Result()>;
|
|
void performTask(Task newTask);
|
|
|
|
//! @return true if the last assigned task is not done yet.
|
|
bool busy() const { return working; }
|
|
Result extractResult() { return std::move(result); }
|
|
|
|
private:
|
|
void run();
|
|
|
|
Task task;
|
|
Result result;
|
|
|
|
ReleaseAcquireAtomic<bool> working { false };
|
|
bool abort { false };
|
|
std::mutex mutex;
|
|
std::condition_variable condition;
|
|
std::thread thread;
|
|
};
|
|
|
|
template<typename Result>
|
|
WorkerThread<Result>::~WorkerThread()
|
|
{
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
abort = true;
|
|
}
|
|
condition.notify_one();
|
|
if (thread.joinable())
|
|
thread.join();
|
|
}
|
|
|
|
template<typename Result>
|
|
void WorkerThread<Result>::performTask(Task newTask)
|
|
{
|
|
assert(!working && "Don't interrupt my work!");
|
|
assert(newTask && "The task may not be empty.");
|
|
task = std::move(newTask);
|
|
|
|
if (thread.joinable()) {
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
working = true;
|
|
}
|
|
condition.notify_one();
|
|
} else {
|
|
working = true;
|
|
thread = std::thread(&WorkerThread::run, this);
|
|
}
|
|
}
|
|
|
|
template<typename Result>
|
|
void WorkerThread<Result>::run()
|
|
{
|
|
while (true) {
|
|
result = task();
|
|
working = false;
|
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
condition.wait(lock, [this] { return working || abort; });
|
|
if (abort)
|
|
break;
|
|
}
|
|
}
|
|
|
|
#endif // WORKER_THREAD_H
|