Skip to content
Snippets Groups Projects
Commit b1663e74 authored by Craig Tiller's avatar Craig Tiller
Browse files

Remove a major source of contention in thread_pool

- notify_one when new work is available to avoid thundering herd
  problems
- checking conditions before entering cv_.wait() prevents a case that
  these are not validated
parent 72784331
No related branches found
No related tags found
No related merge requests found
......@@ -41,6 +41,7 @@ ThreadPool::ThreadPool(int num_threads) {
for (;;) {
std::unique_lock<std::mutex> lock(mu_);
// Wait until work is available or we are shutting down.
if (!shutdown_ || callbacks_.empty())
cv_.wait(lock, [=]() { return shutdown_ || !callbacks_.empty(); });
// Drain callbacks before considering shutdown to ensure all work
// gets completed.
......@@ -71,7 +72,7 @@ ThreadPool::~ThreadPool() {
void ThreadPool::ScheduleCallback(const std::function<void()> &callback) {
std::lock_guard<std::mutex> lock(mu_);
callbacks_.push(callback);
cv_.notify_all();
cv_.notify_one();
}
} // namespace grpc
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment