Simple generic parallelism idiom & C++17 specifics - Bert Hubert's writings
a day ago
- The problem: a loop that is too slow and needs to be parallelized across multiple threads.
- A simple, language-independent idiom uses an atomic counter and a for-loop to fairly distribute work among threads.
- The for-loop in the worker lambda repeatedly gets the next index from the atomic counter until all items are processed.
- Threads are launched and joined manually, giving full control over the number of threads.
- C++17 offers parallel algorithms like std::for_each with std::execution::par, which can be a one-liner.
- C++17 parallel algorithms may require the OneTBB library and may not always actually parallelize (e.g., on std::map).
- The atomic counter idiom is robust and works in any language with atomics and threads.
- Testing is essential because parallelization may not speed up code due to memory access limits.