Skip to content

Commit 43be025

Browse files
committed
Thread/ThreadPool: Fix data race
1 parent 604280c commit 43be025

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

modules/Thread/ThreadPool.mpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,19 @@ export namespace CppUtils::Thread
104104
if (std::empty(tasksQueueAccessor.value()))
105105
return;
106106

107+
auto lockGuard = std::lock_guard{m_waitingMutex};
107108
m_activeWorkers.fetch_add(1, std::memory_order_relaxed);
108109
task = std::move(tasksQueueAccessor->front());
109110
tasksQueueAccessor->pop();
110111
}
111112

112113
task();
113-
m_activeWorkers.fetch_sub(1, std::memory_order_relaxed);
114114

115-
if (m_activeWorkers.load(std::memory_order_relaxed) == 0)
116115
{
117116
auto lockGuard = std::lock_guard{m_waitingMutex};
118-
if (isTasksQueueEmpty())
117+
m_activeWorkers.fetch_sub(1, std::memory_order_relaxed);
118+
119+
if (m_activeWorkers.load(std::memory_order_relaxed) == 0 and isTasksQueueEmpty())
119120
m_waitUntilFinishedCondition.notify_all();
120121
}
121122
}

0 commit comments

Comments
 (0)