Handle spurious wakeups on worker thread condition variables properly

This commit is contained in:
Silent
2019-08-11 14:03:53 +02:00
parent 6ac41a5767
commit 0639ffcfb5
+11 -3
View File
@@ -27,7 +27,7 @@ void WorkerThread::Process(std::function<void()> work) {
void WorkerThread::WaitForCompletion() {
std::unique_lock<std::mutex> guard(doneMutex);
if (jobsDone < jobsTarget) {
while (jobsDone < jobsTarget) {
done.wait(guard);
}
}
@@ -37,7 +37,11 @@ void WorkerThread::WorkFunc() {
std::unique_lock<std::mutex> guard(mutex);
started = true;
while (active) {
signal.wait(guard);
// 'active == false' is one of the conditions for signaling,
// do not "optimize" it
while (active && jobsTarget <= jobsDone) {
signal.wait(guard);
}
if (active) {
work_();
doneMutex.lock();
@@ -67,7 +71,11 @@ void LoopWorkerThread::WorkFunc() {
std::unique_lock<std::mutex> guard(mutex);
started = true;
while (active) {
signal.wait(guard);
// 'active == false' is one of the conditions for signaling,
// do not "optimize" it
while (active && jobsTarget <= jobsDone) {
signal.wait(guard);
}
if (active) {
work_(start_, end_);
doneMutex.lock();