From 335b97df8c610e99befea20ef67ce2f8866eef24 Mon Sep 17 00:00:00 2001 From: Vijay Pai Date: Wed, 25 Mar 2015 14:44:01 -0700 Subject: [PATCH] Revert "Remove lambda expression to support older compilers" This reverts commit 5d5b1d8c8ec4c578f72a12f5b81f705764cdf804. --- src/cpp/server/thread_pool.cc | 41 +++++++++++++++++------------------ src/cpp/server/thread_pool.h | 2 -- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/cpp/server/thread_pool.cc b/src/cpp/server/thread_pool.cc index 10dceec8362..d3013b806c0 100644 --- a/src/cpp/server/thread_pool.cc +++ b/src/cpp/server/thread_pool.cc @@ -35,29 +35,28 @@ namespace grpc { -#ifdef __GNUC__ -#if (__GNUC__ * 100 + __GNUC_MINOR__ < 406) -#define GRPC_NO_NULLPTR -#endif -#endif - -#ifdef GRPC_NO_NULLPTR -#include -const class { -public: - template operator T*() const {return static_cast(0);} - template operator std::unique_ptr() const { - return std::unique_ptr(static_cast(0)); - } - operator bool() const {return false;} -private: - void operator&() const = delete; -} nullptr = {}; -#endif - ThreadPool::ThreadPool(int num_threads) : shutdown_(false) { for (int i = 0; i < num_threads; i++) { - threads_.push_back(std::thread(&ThreadPool::ThreadFunc, this)); + threads_.push_back(std::thread([this]() { + for (;;) { + // Wait until work is available or we are shutting down. + auto have_work = [this]() { return shutdown_ || !callbacks_.empty(); }; + std::unique_lock lock(mu_); + if (!have_work()) { + cv_.wait(lock, have_work); + } + // Drain callbacks before considering shutdown to ensure all work + // gets completed. + if (!callbacks_.empty()) { + auto cb = callbacks_.front(); + callbacks_.pop(); + lock.unlock(); + cb(); + } else if (shutdown_) { + return; + } + } + })); } } diff --git a/src/cpp/server/thread_pool.h b/src/cpp/server/thread_pool.h index 41e2009ff16..6225d82a0b6 100644 --- a/src/cpp/server/thread_pool.h +++ b/src/cpp/server/thread_pool.h @@ -58,8 +58,6 @@ class ThreadPool GRPC_FINAL : public ThreadPoolInterface { bool shutdown_; std::queue> callbacks_; std::vector threads_; - - void ThreadFunc(); }; } // namespace grpc