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
This commit is contained in:
parent
72784331ae
commit
b1663e7465
|
|
@ -41,7 +41,8 @@ ThreadPool::ThreadPool(int num_threads) {
|
|||
for (;;) {
|
||||
std::unique_lock<std::mutex> lock(mu_);
|
||||
// Wait until work is available or we are shutting down.
|
||||
cv_.wait(lock, [=]() { return shutdown_ || !callbacks_.empty(); });
|
||||
if (!shutdown_ || callbacks_.empty())
|
||||
cv_.wait(lock, [=]() { return shutdown_ || !callbacks_.empty(); });
|
||||
// Drain callbacks before considering shutdown to ensure all work
|
||||
// gets completed.
|
||||
if (!callbacks_.empty()) {
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue