From b81df7e000ca4010589b6bee09c437a7e97118c1 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Sun, 1 Feb 2015 22:15:43 -0800 Subject: [PATCH] Fix an obvious bug And make it easy not to make the same mistake --- src/cpp/server/thread_pool.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cpp/server/thread_pool.cc b/src/cpp/server/thread_pool.cc index 35d61244f2e..20279592cbc 100644 --- a/src/cpp/server/thread_pool.cc +++ b/src/cpp/server/thread_pool.cc @@ -41,8 +41,10 @@ ThreadPool::ThreadPool(int num_threads) { for (;;) { std::unique_lock lock(mu_); // Wait until work is available or we are shutting down. - if (!shutdown_ || callbacks_.empty()) - cv_.wait(lock, [=]() { return shutdown_ || !callbacks_.empty(); }); + auto have_work = [=]() { return shutdown_ || !callbacks_.empty(); }; + if (!have_work()) { + cv_.wait(lock, have_work); + } // Drain callbacks before considering shutdown to ensure all work // gets completed. if (!callbacks_.empty()) {