progschj / ThreadPool

A simple C++11 Thread Pool implementation
zlib License
7.85k stars 2.24k forks source link

Race Condition in ThreadPool Constructor #23

Closed thejacobpollack closed 9 years ago

thejacobpollack commented 9 years ago

There's a race condition for the shared resource task in the ThreadPool constructor. Move the variable definition outside of the infinite loop and add the C++11 thread_local storage duration specifier:

thread_local std::function<void()> task;
progschj commented 9 years ago

The task object is local to the lambda (and hence the thread) so no other thread even knows about it. So I'm fairly sure it is fine as it is.

thejacobpollack commented 9 years ago

You're absolutely right -- I overlooked that. I apologize.