Fix ThreadPool::easyStartAndWait()

Previously, the "waiting" part wasn't implemented.
This commit is contained in:
Stephen Seo 2022-06-16 12:20:02 +09:00
parent 292bffb636
commit 62600cbfa6
2 changed files with 31 additions and 22 deletions

View file

@ -801,7 +801,7 @@ namespace EC
} }
else else
{ {
std::array<TPFnDataStructZero*, ThreadCount * 2> fnDataAr; std::array<TPFnDataStructZero, ThreadCount * 2> fnDataAr;
std::size_t s = currentSize / (ThreadCount * 2); std::size_t s = currentSize / (ThreadCount * 2);
for(std::size_t i = 0; i < ThreadCount * 2; ++i) { for(std::size_t i = 0; i < ThreadCount * 2; ++i) {
@ -815,15 +815,14 @@ namespace EC
if(begin == end) { if(begin == end) {
continue; continue;
} }
fnDataAr[i] = new TPFnDataStructZero{}; fnDataAr[i].range = {begin, end};
fnDataAr[i]->range = {begin, end}; fnDataAr[i].manager = this;
fnDataAr[i]->manager = this; fnDataAr[i].entities = &entities;
fnDataAr[i]->entities = &entities; fnDataAr[i].signature = signatureBitset;
fnDataAr[i]->signature = signatureBitset; fnDataAr[i].userData = userData;
fnDataAr[i]->userData = userData;
for(std::size_t j = begin; j < end; ++j) { for(std::size_t j = begin; j < end; ++j) {
if(!isAlive(j)) { if(!isAlive(j)) {
fnDataAr[i]->dead.insert(j); fnDataAr[i].dead.insert(j);
} }
} }
@ -845,8 +844,7 @@ namespace EC
data->userData); data->userData);
} }
} }
delete data; }, &fnDataAr[i]);
}, fnDataAr[i]);
} }
threadPool->easyStartAndWait(); threadPool->easyStartAndWait();
} }
@ -1991,7 +1989,7 @@ namespace EC
} }
} }
} else { } else {
std::array<TPFnDataStructZero*, ThreadCount * 2> fnDataAr; std::array<TPFnDataStructZero, ThreadCount * 2> fnDataAr;
std::size_t s = currentSize / (ThreadCount * 2); std::size_t s = currentSize / (ThreadCount * 2);
for(std::size_t i = 0; i < ThreadCount * 2; ++i) { for(std::size_t i = 0; i < ThreadCount * 2; ++i) {
@ -2005,15 +2003,14 @@ namespace EC
if(begin == end) { if(begin == end) {
continue; continue;
} }
fnDataAr[i] = new TPFnDataStructZero{}; fnDataAr[i].range = {begin, end};
fnDataAr[i]->range = {begin, end}; fnDataAr[i].manager = this;
fnDataAr[i]->manager = this; fnDataAr[i].entities = &entities;
fnDataAr[i]->entities = &entities; fnDataAr[i].signature = signatureBitset;
fnDataAr[i]->signature = signatureBitset; fnDataAr[i].userData = userData;
fnDataAr[i]->userData = userData;
for(std::size_t j = begin; j < end; ++j) { for(std::size_t j = begin; j < end; ++j) {
if(!isAlive(j)) { if(!isAlive(j)) {
fnDataAr[i]->dead.insert(j); fnDataAr[i].dead.insert(j);
} }
} }
threadPool->queueFn([&fn] (void *ud) { threadPool->queueFn([&fn] (void *ud) {
@ -2029,8 +2026,7 @@ namespace EC
fn(i, data->manager, data->userData); fn(i, data->manager, data->userData);
} }
} }
delete data; }, &fnDataAr[i]);
}, fnDataAr[i]);
} }
threadPool->easyStartAndWait(); threadPool->easyStartAndWait();
} }

View file

@ -62,7 +62,7 @@ class ThreadPool {
fnQueue.emplace(std::make_tuple(fn, ud)); fnQueue.emplace(std::make_tuple(fn, ud));
} }
void startThreads() { Internal::PointersT startThreads() {
if (MAXSIZE >= 2) { if (MAXSIZE >= 2) {
checkStacks(); checkStacks();
auto pointers = newStackEntry(); auto pointers = newStackEntry();
@ -143,9 +143,11 @@ class ThreadPool {
while (aCounter->load() != MAXSIZE) { while (aCounter->load() != MAXSIZE) {
std::this_thread::sleep_for(std::chrono::microseconds(15)); std::this_thread::sleep_for(std::chrono::microseconds(15));
} }
return pointers;
} else { } else {
sequentiallyRunTasks(); sequentiallyRunTasks();
} }
return {nullptr, nullptr, nullptr};
} }
/*! /*!
@ -163,7 +165,7 @@ class ThreadPool {
void easyStartAndWait() { void easyStartAndWait() {
if (MAXSIZE >= 2) { if (MAXSIZE >= 2) {
startThreads(); Internal::PointersT pointers = startThreads();
do { do {
std::this_thread::sleep_for(std::chrono::microseconds(30)); std::this_thread::sleep_for(std::chrono::microseconds(30));
@ -177,6 +179,17 @@ class ThreadPool {
break; break;
} }
} while (true); } while (true);
if (std::get<0>(pointers)) {
do {
{
std::lock_guard<std::mutex> lock(*std::get<1>(pointers));
if (std::get<0>(pointers)->empty()) {
break;
}
}
std::this_thread::sleep_for(std::chrono::microseconds(15));
} while (true);
}
} else { } else {
sequentiallyRunTasks(); sequentiallyRunTasks();
} }