From 16f410c8ef111ffcc69de3a1eee6ed9fadd832cb Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 7 Sep 2021 11:29:06 +0900 Subject: [PATCH] Add isQueueEmpty() to ThreadPool --- src/EC/Manager.hpp | 20 ++++++++++---------- src/EC/ThreadPool.hpp | 5 +++++ src/test/ThreadPoolTest.cpp | 4 ++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index ea07b04..4e5b57a 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -694,7 +694,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } } @@ -813,7 +813,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } } @@ -957,7 +957,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } }))); @@ -1036,7 +1036,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } return matchingV; @@ -1415,7 +1415,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } // call functions on matching entities @@ -1477,7 +1477,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } } ); @@ -1608,7 +1608,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } // call functions on matching entities @@ -1675,7 +1675,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } } ); @@ -1739,7 +1739,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } } @@ -1819,7 +1819,7 @@ namespace EC threadPool.wakeThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(200)); - } while(!threadPool.isAllThreadsWaiting()); + } while(!threadPool.isQueueEmpty() && !threadPool.isAllThreadsWaiting()); } } }; diff --git a/src/EC/ThreadPool.hpp b/src/EC/ThreadPool.hpp index f6f9e36..aee681d 100644 --- a/src/EC/ThreadPool.hpp +++ b/src/EC/ThreadPool.hpp @@ -104,6 +104,11 @@ public: return waitCount == THREADCOUNT::value; } + bool isQueueEmpty() { + std::lock_guard lock(queueMutex); + return fnQueue.empty(); + } + private: std::vector threads; std::atomic_bool isAlive; diff --git a/src/test/ThreadPoolTest.cpp b/src/test/ThreadPoolTest.cpp index b695d49..2cec50c 100644 --- a/src/test/ThreadPoolTest.cpp +++ b/src/test/ThreadPoolTest.cpp @@ -24,7 +24,7 @@ TEST(ECThreadPool, Simple) { do { std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } while(!p.isAllThreadsWaiting()); + } while(!p.isQueueEmpty() && !p.isAllThreadsWaiting()); ASSERT_EQ(data.load(), 1); @@ -35,7 +35,7 @@ TEST(ECThreadPool, Simple) { do { std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } while(!p.isAllThreadsWaiting()); + } while(!p.isQueueEmpty() && !p.isAllThreadsWaiting()); ASSERT_EQ(data.load(), 11); } -- 2.49.0