summaryrefslogtreecommitdiff
path: root/src/thread_utils.h
blob: 1eec83a6b34e55a7a421a87720f1cdb7a6311185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef MO2_THREAD_UTILS_H
#define MO2_THREAD_UTILS_H

#include <functional>
#include <log.h>
#include <mutex>
#include <thread>

// in main.cpp
void setExceptionHandlers();

namespace MOShared
{

// starts an std::thread with an unhandled exception handler for core dumps
// and a top-level catch
//
template <class F>
std::thread startSafeThread(F&& f)
{
  return std::thread([f = std::forward<F>(f)] {
    setExceptionHandlers();
    f();
  });
}

/**
 * @brief Apply the given callable to each element between the two given iterators
 *     in a parallel way.
 *
 * The callable should be independent, or properly synchronized, and the source of
 * the range should not change during this call.
 *
 * @param start Beginning of the range.
 * @param end End of the range.
 * @param callable Callable to apply to every element of the range. See std::invoke
 *     requirements. Must be copiable.
 * @param nThreads Number of threads to use.
 *
 */
template <class It, class Callable>
void parallelMap(It begin, It end, Callable callable, std::size_t nThreads)
{
  std::mutex m;
  std::vector<std::thread> threads(nThreads);

  // 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 = startSafeThread([&m, &begin, end, callable]() {
      while (true) {
        decltype(begin) it;
        {
          std::scoped_lock lock(m);
          if (begin == end) {
            break;
          }
          it = begin++;
        }
        if (it != end) {
          std::invoke(callable, *it);
        }
      }
    });
  }

  // Join everything:
  for (auto& t : threads) {
    t.join();
  }
}

}  // namespace MOShared

#endif