How to achieve deterministic multithreading in C / C ++?

As you know, threads are not deterministic as a calculation model. However, in some situations, we would like to take advantage of the performance of parallel computing with multithreading, while preserving the determination of execution while simplifying some general requirements, such as debugging or some specific requirements. I know that it is possible to achieve deterministic multithreading for a specific task, but I look forward to a general and elegant way (i.e. I do not require a lot of trivial engineering work) to achieve this in C / C ++. It doesn't matter what type of solution: in some libraries, on some platforms, using some common methodologies, or in any other ways, this is normal.

+5
source share
2 answers

If you want determinism for debugging, you can try using the Microsoft Research CHESS tool :

CHESS is a tool for searching and playing Heisenbugs in a parallel program. CHESS runs a parallel test multiple times, ensuring that each run performs a different rotation. If interleaving leads to an error, CHESS can play interleaving to improve debugging. CHESS is available for both managed and native programs.

+4
source

You get deterministic multithreading just like you have mutable constants - you don't.

Instead, you use various forms of synchronization (including things like mutexes, semaphores, conditional variables, signals, etc.) to ensure that deterministic results (if necessary) are obtained from non-deterministic code. Of course, the more synchronization you use, the less parallelism you get from the code; therefore you only need minimal synchronization.

How to do this depends on the exact algorithm - there is no "silver bullet", this is the best way to do synchronization, which works for all completely different problems.

+7
source

Source: https://habr.com/ru/post/1201306/


All Articles