From 0fddbba230a3795fb6e5e214485f1c630e797b74 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 6 Aug 2014 15:20:12 +0200 Subject: [PATCH] CThreadPool: Fix a race when starting threads There was a race where threads were themselves responsible for increasing the counter for the number of running threads. This left an open window where the thread was already started, but our counter was not yet increased. The effect of this race would be that we start more threads than we should. Fix this by increasing the counter before actually starting new worker threads. Signed-off-by: Uli Schlachter --- src/Threads.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Threads.cpp b/src/Threads.cpp index ad3b9312..492056cb 100644 --- a/src/Threads.cpp +++ b/src/Threads.cpp @@ -104,7 +104,7 @@ bool CThreadPool::threadNeeded() const { void CThreadPool::threadFunc() { CMutexLocker guard(m_mutex); - m_num_threads++; + // m_num_threads was already increased m_num_idle++; while (true) { @@ -152,6 +152,7 @@ void CThreadPool::addJob(CJob *job) { return; // Start a new thread for our pool + m_num_threads++; CThread::startThread(threadPoolFunc, this); }