Skip to content

Commit 0fb9215

Browse files
wilxprogschj
authored andcommitted
Do not call unlock() manually.
1 parent 588b59c commit 0fb9215

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

ThreadPool.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,18 @@ inline ThreadPool::ThreadPool(size_t threads)
4040
{
4141
for(;;)
4242
{
43-
std::unique_lock<std::mutex> lock(this->queue_mutex);
44-
while(!this->stop && this->tasks.empty())
45-
this->condition.wait(lock);
46-
if(this->stop && this->tasks.empty())
47-
return;
48-
std::function<void()> task(this->tasks.front());
49-
this->tasks.pop();
50-
lock.unlock();
43+
std::function<void()> task;
44+
45+
{
46+
std::unique_lock<std::mutex> lock(this->queue_mutex);
47+
while(!this->stop && this->tasks.empty())
48+
this->condition.wait(lock);
49+
if(this->stop && this->tasks.empty())
50+
return;
51+
task = std::move(this->tasks.front());
52+
this->tasks.pop();
53+
}
54+
5155
task();
5256
}
5357
}

0 commit comments

Comments
 (0)