aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar vjpai <vpai@google.com>2015-03-25 14:32:30 -0700
committerGravatar vjpai <vpai@google.com>2015-03-25 14:32:30 -0700
commit5d5b1d8c8ec4c578f72a12f5b81f705764cdf804 (patch)
treee29c1d8d7139e83ca5669f4b28fcf5f4af8c0264 /src
parent3ff350cf96676f2c32c6913aceeabf84a15f3715 (diff)
Remove lambda expression to support older compilers
Diffstat (limited to 'src')
-rw-r--r--src/cpp/server/thread_pool.cc41
-rw-r--r--src/cpp/server/thread_pool.h2
2 files changed, 23 insertions, 20 deletions
diff --git a/src/cpp/server/thread_pool.cc b/src/cpp/server/thread_pool.cc
index d3013b806c..10dceec836 100644
--- a/src/cpp/server/thread_pool.cc
+++ b/src/cpp/server/thread_pool.cc
@@ -35,28 +35,29 @@
namespace grpc {
+#ifdef __GNUC__
+#if (__GNUC__ * 100 + __GNUC_MINOR__ < 406)
+#define GRPC_NO_NULLPTR
+#endif
+#endif
+
+#ifdef GRPC_NO_NULLPTR
+#include <memory>
+const class {
+public:
+ template <class T> operator T*() const {return static_cast<T *>(0);}
+ template <class T> operator std::unique_ptr<T>() const {
+ return std::unique_ptr<T>(static_cast<T *>(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([this]() {
- for (;;) {
- // Wait until work is available or we are shutting down.
- auto have_work = [this]() { return shutdown_ || !callbacks_.empty(); };
- std::unique_lock<std::mutex> 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;
- }
- }
- }));
+ threads_.push_back(std::thread(&ThreadPool::ThreadFunc, this));
}
}
diff --git a/src/cpp/server/thread_pool.h b/src/cpp/server/thread_pool.h
index 6225d82a0b..41e2009ff1 100644
--- a/src/cpp/server/thread_pool.h
+++ b/src/cpp/server/thread_pool.h
@@ -58,6 +58,8 @@ class ThreadPool GRPC_FINAL : public ThreadPoolInterface {
bool shutdown_;
std::queue<std::function<void()>> callbacks_;
std::vector<std::thread> threads_;
+
+ void ThreadFunc();
};
} // namespace grpc