From 0f2a98b8867e706d2880f56f9ae7a5a369b9cf02 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 6 Sep 2021 20:57:13 +0900 Subject: [PATCH] Usage of ThreadPool fixes --- src/EC/Manager.hpp | 62 ++++++++++++++++++------------------- src/EC/ThreadPool.hpp | 2 -- src/test/ECTest.cpp | 5 +++ src/test/ThreadPoolTest.cpp | 8 ++--- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index 225ceb8..cdc52aa 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -542,7 +542,7 @@ namespace EC const std::size_t& entityID, CType& ctype, Function* function, - void* userData= nullptr) + void* userData = nullptr) { (*function)( entityID, @@ -692,9 +692,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } } @@ -811,9 +811,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } } @@ -955,9 +955,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } }))); @@ -1034,9 +1034,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } return matchingV; @@ -1413,9 +1413,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } // call functions on matching entities @@ -1475,9 +1475,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } } ); @@ -1606,9 +1606,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } // call functions on matching entities @@ -1673,9 +1673,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } } ); @@ -1737,9 +1737,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } } @@ -1817,9 +1817,9 @@ namespace EC }, &fnDataAr.at(i)); } threadPool.wakeThreads(); - while(!threadPool.isAllThreadsWaiting()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + do { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } while(!threadPool.isAllThreadsWaiting()); } } }; diff --git a/src/EC/ThreadPool.hpp b/src/EC/ThreadPool.hpp index 30969e1..f6f9e36 100644 --- a/src/EC/ThreadPool.hpp +++ b/src/EC/ThreadPool.hpp @@ -87,13 +87,11 @@ public: } void wakeThreads(bool wakeAll = true) { - unsigned int counter = 0; if(wakeAll) { cv.notify_all(); } else { cv.notify_one(); } - while(isAllThreadsWaiting() && counter++ < 10000) {} } int getWaitCount() { diff --git a/src/test/ECTest.cpp b/src/test/ECTest.cpp index 3105903..a59fa98 100644 --- a/src/test/ECTest.cpp +++ b/src/test/ECTest.cpp @@ -458,6 +458,11 @@ TEST(EC, MultiThreaded) EXPECT_EQ(0, manager.getEntityData(i)->y); } +#ifndef NDEBUG + std::clog << "Addr of C0 for entity 0 is " << manager.getEntityData(0) + << std::endl; +#endif + manager.forMatchingSignature >( [] (const std::size_t& /* id */, void* /* context */, C0* c) { c->x = 1; diff --git a/src/test/ThreadPoolTest.cpp b/src/test/ThreadPoolTest.cpp index ef7a695..b695d49 100644 --- a/src/test/ThreadPoolTest.cpp +++ b/src/test/ThreadPoolTest.cpp @@ -22,9 +22,9 @@ TEST(ECThreadPool, Simple) { p.wakeThreads(); - while(!p.isAllThreadsWaiting()) { + do { std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } + } while(!p.isAllThreadsWaiting()); ASSERT_EQ(data.load(), 1); @@ -33,9 +33,9 @@ TEST(ECThreadPool, Simple) { } p.wakeThreads(); - while(!p.isAllThreadsWaiting()) { + do { std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } + } while(!p.isAllThreadsWaiting()); ASSERT_EQ(data.load(), 11); }