You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by jb...@apache.org on 2019/01/09 22:08:37 UTC

[geode-native] branch develop updated: GEODE-2484: Fixes hang in ThreadPool on shutdown. (#434)

This is an automated email from the ASF dual-hosted git repository.

jbarrett pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-native.git


The following commit(s) were added to refs/heads/develop by this push:
     new 4f5eed7  GEODE-2484: Fixes hang in ThreadPool on shutdown. (#434)
4f5eed7 is described below

commit 4f5eed708e2a2bc4c08ecbfbb5438dbc23181783
Author: Jacob Barrett <jb...@pivotal.io>
AuthorDate: Wed Jan 9 14:08:33 2019 -0800

    GEODE-2484: Fixes hang in ThreadPool on shutdown. (#434)
---
 cppcache/src/ThreadPool.cpp | 30 +++++++++++++++++++-----------
 cppcache/src/ThreadPool.hpp |  2 +-
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/cppcache/src/ThreadPool.cpp b/cppcache/src/ThreadPool.cpp
index b74bf24..a20af5c 100644
--- a/cppcache/src/ThreadPool.cpp
+++ b/cppcache/src/ThreadPool.cpp
@@ -57,22 +57,30 @@ ThreadPool::ThreadPool(size_t threadPoolSize) : shutdown_(false) {
 ThreadPool::~ThreadPool() { shutDown(); }
 
 void ThreadPool::perform(std::shared_ptr<Callable> req) {
-  std::unique_lock<decltype(queueMutex_)> lock(queueMutex_);
-  auto wasEmpty = queue_.empty();
-  queue_.push_back(std::move(req));
-  lock.unlock();
-
-  if (wasEmpty) {
-    queueCondition_.notify_all();
+  {
+    std::lock_guard<decltype(queueMutex_)> lock(queueMutex_);
+    queue_.push_back(std::move(req));
+    if (queue_.size() > 1) {
+      return;
+    }
   }
+
+  queueCondition_.notify_all();
 }
 
 void ThreadPool::shutDown(void) {
-  if (!shutdown_.exchange(true)) {
-    queueCondition_.notify_all();
-    for (auto& worker : workers_) {
-      worker.join();
+  {
+    std::lock_guard<decltype(queueMutex_)> lock(queueMutex_);
+    if (shutdown_) {
+      return;
     }
+    shutdown_ = true;
+  }
+
+  queueCondition_.notify_all();
+
+  for (auto& worker : workers_) {
+    worker.join();
   }
 }
 
diff --git a/cppcache/src/ThreadPool.hpp b/cppcache/src/ThreadPool.hpp
index b59fe46..a8dd49d 100644
--- a/cppcache/src/ThreadPool.hpp
+++ b/cppcache/src/ThreadPool.hpp
@@ -84,7 +84,7 @@ class ThreadPool {
   void shutDown(void);
 
  private:
-  std::atomic<bool> shutdown_;
+  bool shutdown_;
   std::vector<std::thread> workers_;
   std::deque<std::shared_ptr<Callable>> queue_;
   std::mutex queueMutex_;