This question has already been answered:

Hello to all! How to debug multithreaded programs in Qt? Are there any examples or information for consideration?

Reported as a duplicate by gbg , user194374, aleksandr barakin , Denis , αλεχολυτ Jul 16 '16 at 12:51 pm .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    The best way to debug such programs is detailed logging. Because with a debugger, some combinations of states will be impossible to catch.

    Additional ways:

    • write protected code - check everything that can be checked with assert()
    • be sure to check the return code of the system calls
    • use RAII tight
    • arm with a static analyzer
    • and dynamic too - Valgrind, for example
    • The disadvantage of this approach may be the fact that with detailed logging errors may go away, or others may appear. Debugging multithreaded applications is hell, just hell. - ixSci
    • @ixSci needs to log into something terribly fast and reliable. For example, in the queue with the lock-free mechanism. - gbg
    • Anyway, it will give some kind of confusion in time and the bug may just disappear. I'd rather use the debugger capabilities to track the state of the memory. In general, this is a really difficult topic, the author has yet to realize this. - ixSci
    • one
      The best way is not to write multi-threaded programs at all. If you really want, then the threads should not work with shared memory (I'm not kidding, everyone works only with its own set of variables, a range of array elements, etc.). For individual messages it is better to use pipes (and the like kernel tools). - avp
    • one
      @gbg, too often the expected performance is not achieved (everything eats up sync). If the work is large, then usually they can be decomposed in general. - avp