summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMikaël Capelle <capelle.mikael@gmail.com>2020-05-24 14:40:44 +0200
committerMikaël Capelle <capelle.mikael@gmail.com>2020-05-24 14:40:44 +0200
commit001e44fec45be3fe94d5075812547c0277f2e6d1 (patch)
tree810d35c9109b8bf985dd122ad403a980dbbbf87f /src
parentff4fbbdc5fa5ada01bd5bc3d8f8004279d7cc6b8 (diff)
Minor cleaning.
Diffstat (limited to 'src')
-rw-r--r--src/thread_utils.h9
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();