Click to See Complete Forum and Search --> : Debugging MultiThreaded Applications


sgonepudi
March 10th, 2006, 08:50 AM
How to debug Multithread Applications in Windows Environment? Right now I am using VC++ 6.0. VC++ debugger is allowing only one thread at a time to debug.

If any body knows about this let me know the Techniques for Debugging.

Arjay
March 10th, 2006, 01:02 PM
You will always only be able to break on one thread at a time in the debugger. You can set break points in multiple threads, but the debugger is only going to be able to stop at one of them at any given time.

One thing that I find helpful is to debug specific areas of my program. I'll start in the main thread where the data is passed or shared to a secondary thread and set a breakpoint. I make sure the data passed to the secondary thread is correct. After that, I set a break point in the secondary thread proc, and verify the data that was passed is correct.

I usually wrap any data that's being shared between two threads into a class that provides synchronization to the data. Say I'm using an std::queue to pass data from one thread to another. I'll create a class that provides accessor methods, synchonization objects and synchronization within the accessor methods. An instance of this class is created in the primary thread, and passed to the secondary thread.

When each thread, needs to access the queue it simply calls the accessor methods without having to worry about the details of synchronization. It makes the calling code in the pri and sec threads much simpler. In addition, I usually use a class wrapper for the critical sections so I don't need to make explicit create, enter, leave, and delete calls. In my opinion, the majority of synchronization issues arise from incorrect initialization of synch primitives and/or incorrect locking and unlocking. Using synch primitive wrappers greatly reduce the chance of this happening and makes the code cleaner. For more info, check out the articles listed in my signature.

Since the debugger only can take you so far, I find that logging to file or debug output helpful to determine if things are happening in the right order.

Arjay

Need a little help with Win32 thread synchronization? Check out the following CG articles:

Win32 Thread Synchronization, Part I: Overview (http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c9823/)
Win32 Thread Synchronization, Part 2: Helper Classes (http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c9825/)

MikeAThon
March 10th, 2006, 07:07 PM
In VC++ 6.0, while halted in a breakpoint, you can change focus to a different thread so as to see the call stack for the other thread. It's so useful to me that I have customized the toolbar to include a button for it. It's found from the main menu under Debug->Threads

To use it effectively, if you have more than a few threads, you must also name your threads (so you know which is which). See "Setting a Thread Name in Unmanged Code" at http://msdn.microsoft.com/library/en-us/vsdebug/html/vxtskSettingThreadName.asp

Mike