diff options
| author | Mikaël Capelle <capelle.mikael@gmail.com> | 2020-05-24 14:40:44 +0200 |
|---|---|---|
| committer | Mikaël Capelle <capelle.mikael@gmail.com> | 2020-05-24 14:40:44 +0200 |
| commit | 001e44fec45be3fe94d5075812547c0277f2e6d1 (patch) | |
| tree | 810d35c9109b8bf985dd122ad403a980dbbbf87f | |
| parent | ff4fbbdc5fa5ada01bd5bc3d8f8004279d7cc6b8 (diff) | |
Minor cleaning.
| -rw-r--r-- | src/thread_utils.h | 9 |
1 files changed, 6 insertions, 3 deletions
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 <class It, class Callable> -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<std::thread> 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(); |
