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 <psychon@znc.in>
This commit is contained in:
Uli Schlachter
2014-08-06 15:20:12 +02:00
parent 1d67e87d90
commit 0fddbba230

View File

@@ -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);
}