aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar vjpai <vpai@google.com>2015-07-23 14:41:23 -0700
committerGravatar vjpai <vpai@google.com>2015-07-23 14:41:23 -0700
commitb76f3ada1177f8ae4ce749a6305b55c3552fb92a (patch)
tree7f5333801f1bfa51bdfc3ad42776097c954c6232
parentb28456b1e46085bd35b6389b03e6d4de8866bdaf (diff)
Fix bug on shutdown case
-rw-r--r--include/grpc++/dynamic_thread_pool.h1
-rw-r--r--src/cpp/server/dynamic_thread_pool.cc13
2 files changed, 9 insertions, 5 deletions
diff --git a/include/grpc++/dynamic_thread_pool.h b/include/grpc++/dynamic_thread_pool.h
index 7519a47726..e01063ced2 100644
--- a/include/grpc++/dynamic_thread_pool.h
+++ b/include/grpc++/dynamic_thread_pool.h
@@ -64,6 +64,7 @@ class DynamicThreadPool GRPC_FINAL : public ThreadPoolInterface {
};
grpc::mutex mu_;
grpc::condition_variable cv_;
+ grpc::condition_variable shutdown_cv_;
bool shutdown_;
std::queue<std::function<void()>> callbacks_;
int reserve_threads_;
diff --git a/src/cpp/server/dynamic_thread_pool.cc b/src/cpp/server/dynamic_thread_pool.cc
index bc0d16f170..c42563103c 100644
--- a/src/cpp/server/dynamic_thread_pool.cc
+++ b/src/cpp/server/dynamic_thread_pool.cc
@@ -58,6 +58,9 @@ void DynamicThreadPool::DynamicThread::ThreadFunc() {
pool_->dead_threads_.push_back(this);
}
}
+ if ((pool_->shutdown_) && (pool_->nthreads_ == 0)) {
+ pool_->shutdown_cv_.notify_one();
+ }
}
void DynamicThreadPool::ThreadFunc() {
@@ -103,12 +106,12 @@ void DynamicThreadPool::ReapThreads(std::list<DynamicThread*>* tlist) {
}
DynamicThreadPool::~DynamicThreadPool() {
- {
- grpc::lock_guard<grpc::mutex> lock(mu_);
- shutdown_ = true;
- cv_.notify_all();
+ grpc::unique_lock<grpc::mutex> lock(mu_);
+ shutdown_ = true;
+ cv_.notify_all();
+ while (nthreads_ != 0) {
+ shutdown_cv_.wait(lock);
}
- ReapThreads(&live_threads_);
ReapThreads(&dead_threads_);
}