Fix the counter in for_each_index_with_workers going out of scope before workers finish.

This commit is contained in:
Tomasz Sobczyk
2020-10-30 10:36:39 +01:00
committed by nodchip
parent a56d8124d8
commit 8c81bbd3db
+7 -2
View File
@@ -135,10 +135,15 @@ struct ThreadPool : public std::vector<Thread*> {
typename Detail::TypeIdentity<IndexT>::Type end, typename Detail::TypeIdentity<IndexT>::Type end,
FuncT func) FuncT func)
{ {
std::atomic<IndexT> i_atomic = begin; // This value must outlive the function call.
// It's fairly safe if we make it static
// because for_each_index_with_workers
// is not reentrant nor thread safe.
static std::atomic<IndexT> i_atomic;
i_atomic.store(begin);
execute_with_workers( execute_with_workers(
[&i_atomic, end, func](Thread& th) mutable { [end, func](Thread& th) mutable {
for(;;) { for(;;) {
const auto i = i_atomic.fetch_add(1); const auto i = i_atomic.fetch_add(1);
if (i >= end) if (i >= end)