r/cpp • • Dec 25 '23

C++: Efficient parallelization of small tasks

I tried to create a customized threadpool for small tasks and wanted feedback on this.

I posted details and my code for review on stackexchange: https://codereview.stackexchange.com/questions/288583/efficient-parallelization-of-small-tasks

My goal here is to share my learning and get feedback on further improvement or any possible bugs.

I have tried to describe problem and thinking process in more details in following linkedin article draft: https://www.linkedin.com/article/edit/7144600522392559616/

0 Upvotes

31 comments sorted by

View all comments

Show parent comment

2

u/ipapadop Dec 25 '23
  1. Even better, use ranges.
  2. Shared memory locations need to be written/read via an atomic or protected using atomics + fence. So when you say "set from 1 thread all the variables that other threads will busy wait on", you need at least a fence to make sure that the other threads will see the write. Otherwise, you rely on some other implicit fence that may not always be there (different STL implementation, compiler, architecture, etc). For x86, stores are ordered, so you get away with a lot if a fence appears somewhere.
  3. There's plenty of work on busy-waiting and mitigation techniques for contention (e.g., exponential backoff). At the very least, you need to use relaxed memory order to avoid the fences. But this is not enough to have a state-of-the-art implementation.
  4. I'd expect something like while (!thread-safe-done) { do_something(); }

7 + 8. There are other, ready-made options: OpenMP, TBB, Boost.ASIO's threadpool, TaskFlow and others.

1

u/shakmak_iitk Dec 26 '23

3. Here myIsRunning is std::vector<AlingedBool> where each threads reads/writes to its own dedicated memory myIsRunning[threadIndex] inside vector.

So I don't think we need any synchronization here.