From: Stephen Seo Date: Tue, 7 Sep 2021 02:29:06 +0000 (+0900) Subject: Add isQueueEmpty() to ThreadPool X-Git-Tag: 1.0~35^2~2 X-Git-Url: https://git.seodisparate.com/stephenseo/static/search/searchdata.js?a=commitdiff_plain;h=16f410c8ef111ffcc69de3a1eee6ed9fadd832cb;p=EntityComponentMetaSystem Add isQueueEmpty() to ThreadPool --- 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); }