From 61b170845f256b1977c6d8a8a5906ba998560a53 Mon Sep 17 00:00:00 2001 From: Jim King Date: Tue, 19 Apr 2016 15:57:31 -0400 Subject: [PATCH] THRIFT-3233 fix accounting of workers in thread manager Client: C++ Patch: Jim King This closes #992 --- .../src/thrift/concurrency/ThreadManager.cpp | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/cpp/src/thrift/concurrency/ThreadManager.cpp b/lib/cpp/src/thrift/concurrency/ThreadManager.cpp index 71b8fec57..24bfeec44 100644 --- a/lib/cpp/src/thrift/concurrency/ThreadManager.cpp +++ b/lib/cpp/src/thrift/concurrency/ThreadManager.cpp @@ -213,8 +213,6 @@ public: */ void run() { bool active = false; - bool notifyManager = false; - /** * Increment worker semaphore and notify manager if worker count reached * desired max @@ -223,18 +221,20 @@ public: * since that is what the manager blocks on for worker add/remove */ { - Synchronized s(manager_->monitor_); - active = manager_->workerCount_ < manager_->workerMaxCount_; - if (active) { - manager_->workerCount_++; - notifyManager = manager_->workerCount_ == manager_->workerMaxCount_; + bool notifyManager = false; + { + Synchronized s(manager_->monitor_); + active = manager_->workerCount_ < manager_->workerMaxCount_; + if (active) { + manager_->workerCount_++; + notifyManager = manager_->workerCount_ == manager_->workerMaxCount_; + } } - } - if (notifyManager) { - Synchronized s(manager_->workerMonitor_); - manager_->workerMonitor_.notify(); - notifyManager = false; + if (notifyManager) { + Synchronized s(manager_->workerMonitor_); + manager_->workerMonitor_.notify(); + } } while (active) { @@ -279,10 +279,6 @@ public: && manager_->tasks_.size() <= manager_->pendingTaskCountMax_ - 1) { manager_->maxMonitor_.notify(); } - } else { - idle_ = true; - manager_->workerCount_--; - notifyManager = (manager_->workerCount_ == manager_->workerMaxCount_); } } @@ -302,6 +298,9 @@ public: { Synchronized s(manager_->workerMonitor_); manager_->deadWorkers_.insert(this->thread()); + idle_ = true; + manager_->workerCount_--; + bool notifyManager = (manager_->workerCount_ == manager_->workerMaxCount_); if (notifyManager) { manager_->workerMonitor_.notify(); }