Debugging multitasking programs

I've been programming in C for many years, and my favorite "debugger" has always been the printf () function - I resort to the studio's visual debugger when absolutely necessary, and therefore have never been very proficient in using it. I recently had to modify a program from C to C ++ (although of course printf still works fine) and parts of the program are now being processed by multiple threads (one for each core on a multi-core computer) to get the program running Faster. Now I will no doubt run into awkward multithreading bugs like deadlocks and am wondering what debugging methodology I can turn to. Does visual studio (2008) have anything I could reasonably do to help me resolve thread related errors? Should I take some time to find outhow to use a third party debugger? Can I solve most problems using good old printf?

Can I, for example, write code that if it waits for a critical section to enter, it prints something like "Thread X waiting for input ... but blocked because it is being used by thread Y"?

+2


source to share


4 answers


Visual Studio supports debugging threads to some extent. Through the streams window, you can select streams, pause and resume streams, etc. When you switch between threads, the call stack window updates accordingly, so you can check what each thread is doing. You can also restrict breakpoints to specific threads.

If you want an alternative WinDbg (which is part of Microsoft's free Debug for Windows package ) offers many options but with a slightly more esoteric user interface.



As far as usage goes printf

, there is a problem of synchronizing the output. If you don't, you are more likely to gibberish. If you keep it in sync, you are basically changing the concurrency of the application, which may or may not affect the problem you are trying to solve.

+10


source


If you can port your project to Linux, Valgrind (especially the "helgrind" tool) will do exactly what you ask. http://valgrind.org/



0


source


I'm not sure if this is exactly what you are asking, but to aid in debugging you can write code that gives each thread a "name" so that debug messages are printed in the debug window (or a log file or whatever) include this "name" of the thread along with any other information you specify. The code is in C #, but it is available even in unmanaged C ++

  Thread T = new Thread(RunSchedule);
  T.Name = "Scheduler";    // <=== Thread given a name here... 
  T.Start();

      

0


source


Intel provides several tools for finding thread-related problems: data calculations, deadlocks, performance penalties. These tools are: Intel Thread Checker, Intel Thread Profiler.

0


source







All Articles