From 62600cbfa6eb6565aaa8f98de6431cdbe7411648 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 16 Jun 2022 12:20:02 +0900 Subject: [PATCH] Fix ThreadPool::easyStartAndWait() Previously, the "waiting" part wasn't implemented. --- src/EC/Manager.hpp | 36 ++++++++++++++++-------------------- src/EC/ThreadPool.hpp | 17 +++++++++++++++-- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index ad8d382..11c9134 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -801,7 +801,7 @@ namespace EC } else { - std::array fnDataAr; + std::array fnDataAr; std::size_t s = currentSize / (ThreadCount * 2); for(std::size_t i = 0; i < ThreadCount * 2; ++i) { @@ -815,15 +815,14 @@ namespace EC if(begin == end) { continue; } - fnDataAr[i] = new TPFnDataStructZero{}; - fnDataAr[i]->range = {begin, end}; - fnDataAr[i]->manager = this; - fnDataAr[i]->entities = &entities; - fnDataAr[i]->signature = signatureBitset; - fnDataAr[i]->userData = userData; + fnDataAr[i].range = {begin, end}; + fnDataAr[i].manager = this; + fnDataAr[i].entities = &entities; + fnDataAr[i].signature = signatureBitset; + fnDataAr[i].userData = userData; for(std::size_t j = begin; j < end; ++j) { if(!isAlive(j)) { - fnDataAr[i]->dead.insert(j); + fnDataAr[i].dead.insert(j); } } @@ -845,8 +844,7 @@ namespace EC data->userData); } } - delete data; - }, fnDataAr[i]); + }, &fnDataAr[i]); } threadPool->easyStartAndWait(); } @@ -1991,7 +1989,7 @@ namespace EC } } } else { - std::array fnDataAr; + std::array fnDataAr; std::size_t s = currentSize / (ThreadCount * 2); for(std::size_t i = 0; i < ThreadCount * 2; ++i) { @@ -2005,15 +2003,14 @@ namespace EC if(begin == end) { continue; } - fnDataAr[i] = new TPFnDataStructZero{}; - fnDataAr[i]->range = {begin, end}; - fnDataAr[i]->manager = this; - fnDataAr[i]->entities = &entities; - fnDataAr[i]->signature = signatureBitset; - fnDataAr[i]->userData = userData; + fnDataAr[i].range = {begin, end}; + fnDataAr[i].manager = this; + fnDataAr[i].entities = &entities; + fnDataAr[i].signature = signatureBitset; + fnDataAr[i].userData = userData; for(std::size_t j = begin; j < end; ++j) { if(!isAlive(j)) { - fnDataAr[i]->dead.insert(j); + fnDataAr[i].dead.insert(j); } } threadPool->queueFn([&fn] (void *ud) { @@ -2029,8 +2026,7 @@ namespace EC fn(i, data->manager, data->userData); } } - delete data; - }, fnDataAr[i]); + }, &fnDataAr[i]); } threadPool->easyStartAndWait(); } diff --git a/src/EC/ThreadPool.hpp b/src/EC/ThreadPool.hpp index 74f16da..003a617 100644 --- a/src/EC/ThreadPool.hpp +++ b/src/EC/ThreadPool.hpp @@ -62,7 +62,7 @@ class ThreadPool { fnQueue.emplace(std::make_tuple(fn, ud)); } - void startThreads() { + Internal::PointersT startThreads() { if (MAXSIZE >= 2) { checkStacks(); auto pointers = newStackEntry(); @@ -143,9 +143,11 @@ class ThreadPool { while (aCounter->load() != MAXSIZE) { std::this_thread::sleep_for(std::chrono::microseconds(15)); } + return pointers; } else { sequentiallyRunTasks(); } + return {nullptr, nullptr, nullptr}; } /*! @@ -163,7 +165,7 @@ class ThreadPool { void easyStartAndWait() { if (MAXSIZE >= 2) { - startThreads(); + Internal::PointersT pointers = startThreads(); do { std::this_thread::sleep_for(std::chrono::microseconds(30)); @@ -177,6 +179,17 @@ class ThreadPool { break; } } while (true); + if (std::get<0>(pointers)) { + do { + { + std::lock_guard lock(*std::get<1>(pointers)); + if (std::get<0>(pointers)->empty()) { + break; + } + } + std::this_thread::sleep_for(std::chrono::microseconds(15)); + } while (true); + } } else { sequentiallyRunTasks(); }