Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As we know, threads are non-deterministic as a model of computation. However, in some situations, we would like to take advantage of the performance gain with parallel computing by multithreading, while keeping the execution deterministic at the same time to make it easy for some general demands such as debugging or some specific demands. I know it is possible to achieve deterministic multithreading for a particular task, but I am looking forward to a general and elegant way (that is to say, not require lots of trivial engineering work) to achieve that in C/C++. It does not matter what type of the solution is: it is okay to achieve it by some libraries, by some platforms, by some general methodologies, or by any other ways.

like image 433
ACcreator Avatar asked Aug 28 '14 06:08

ACcreator


2 Answers

You achieve deterministic multi-threading in the same way you have mutable constants - you don't.

Instead you use various forms of synchronisation (including things like mutexes, semaphores, conditional variables, signals, etc) to ensure you get deterministic results (where needed) from non-deterministic code. Of course the more synchronisation you use the less parallelism you get from the code; so you only want the minimum synchronisation necessary.

How to do this depends on the exact algorithm - there is no "silver bullet" that is the best way of doing synchronisation that works for all of the wildly different problems.

like image 55
Brendan Avatar answered Oct 17 '22 03:10

Brendan


If you want determinism for debugging you can try to use CHESS tool from Microsoft Research:

CHESS is a tool for finding and reproducing Heisenbugs in concurrent programs. CHESS repeatedly runs a concurrent test ensuring that every run takes a different interleaving. If an interleaving results in an error, CHESS can reproduce the interleaving for improved debugging. CHESS is available for both managed and native programs.

like image 43
ks1322 Avatar answered Oct 17 '22 02:10

ks1322