Two Workers Never Wake
Three worker threads run this loop; a fourth thread shuts the pool down.
std::mutex m;
std::condition_variable cv;
std::queue<Job> q;
bool done = false;
// each of 3 workers
for (;;) {
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{ return !q.empty() || done; });
if (done && q.empty()) return;
Job j = std::move(q.front()); q.pop();
lk.unlock();
run(j);
}
// shutdown thread, queue already empty
{ std::lock_guard<std::mutex> lk(m); done = true; }
cv.notify_one();
The queue is empty and all three workers are waiting. What happens at shutdown?
Sign in to answer questions and track your progress
Sign In