From 001e44fec45be3fe94d5075812547c0277f2e6d1 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Sun, 24 May 2020 14:40:44 +0200 Subject: Minor cleaning. --- src/thread_utils.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/thread_utils.h') diff --git a/src/thread_utils.h b/src/thread_utils.h index fe096a36..2cb87a8d 100644 --- a/src/thread_utils.h +++ b/src/thread_utils.h @@ -21,10 +21,14 @@ namespace MOShared { * */ template -void parallelMap(It begin, It end, Callable callable, std::size_t nThreads) { +void parallelMap(It begin, It end, Callable callable, std::size_t nThreads) +{ + std::mutex m; std::vector threads(nThreads); - std::mutex m; + // Create the thread: + // - The mutex is only used to fetch/increment the iterator. + // - The callable is copied in each thread to avoid conflicts. for (auto &thread: threads) { thread = std::thread([&m, &begin, end, callable]() { while (true) { @@ -43,7 +47,6 @@ void parallelMap(It begin, It end, Callable callable, std::size_t nThreads) { }); } - // Join everything: for (auto& t : threads) { t.join(); -- cgit v1.3.1