Usage of ThreadPool fixes

This commit is contained in:
Stephen Seo 2021-09-06 20:57:13 +09:00
parent f44d2f8c7b
commit 0f2a98b886
4 changed files with 40 additions and 37 deletions

View file

@ -542,7 +542,7 @@ namespace EC
const std::size_t& entityID, const std::size_t& entityID,
CType& ctype, CType& ctype,
Function* function, Function* function,
void* userData= nullptr) void* userData = nullptr)
{ {
(*function)( (*function)(
entityID, entityID,
@ -692,9 +692,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
} }
@ -811,9 +811,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
} }
@ -955,9 +955,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
}))); })));
@ -1034,9 +1034,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
return matchingV; return matchingV;
@ -1413,9 +1413,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
// call functions on matching entities // call functions on matching entities
@ -1475,9 +1475,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
} }
); );
@ -1606,9 +1606,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
// call functions on matching entities // call functions on matching entities
@ -1673,9 +1673,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
} }
); );
@ -1737,9 +1737,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
} }
@ -1817,9 +1817,9 @@ namespace EC
}, &fnDataAr.at(i)); }, &fnDataAr.at(i));
} }
threadPool.wakeThreads(); threadPool.wakeThreads();
while(!threadPool.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} } while(!threadPool.isAllThreadsWaiting());
} }
} }
}; };

View file

@ -87,13 +87,11 @@ public:
} }
void wakeThreads(bool wakeAll = true) { void wakeThreads(bool wakeAll = true) {
unsigned int counter = 0;
if(wakeAll) { if(wakeAll) {
cv.notify_all(); cv.notify_all();
} else { } else {
cv.notify_one(); cv.notify_one();
} }
while(isAllThreadsWaiting() && counter++ < 10000) {}
} }
int getWaitCount() { int getWaitCount() {

View file

@ -458,6 +458,11 @@ TEST(EC, MultiThreaded)
EXPECT_EQ(0, manager.getEntityData<C0>(i)->y); EXPECT_EQ(0, manager.getEntityData<C0>(i)->y);
} }
#ifndef NDEBUG
std::clog << "Addr of C0 for entity 0 is " << manager.getEntityData<C0>(0)
<< std::endl;
#endif
manager.forMatchingSignature<EC::Meta::TypeList<C0> >( manager.forMatchingSignature<EC::Meta::TypeList<C0> >(
[] (const std::size_t& /* id */, void* /* context */, C0* c) { [] (const std::size_t& /* id */, void* /* context */, C0* c) {
c->x = 1; c->x = 1;

View file

@ -22,9 +22,9 @@ TEST(ECThreadPool, Simple) {
p.wakeThreads(); p.wakeThreads();
while(!p.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
} } while(!p.isAllThreadsWaiting());
ASSERT_EQ(data.load(), 1); ASSERT_EQ(data.load(), 1);
@ -33,9 +33,9 @@ TEST(ECThreadPool, Simple) {
} }
p.wakeThreads(); p.wakeThreads();
while(!p.isAllThreadsWaiting()) { do {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
} } while(!p.isAllThreadsWaiting());
ASSERT_EQ(data.load(), 11); ASSERT_EQ(data.load(), 11);
} }