views:

178

answers:

6

I am aware whether the same question has already been asked (if so please let me know).When i am working on multithreads how can i debug to know which thread causes an abnormal behavior ?

could i need to use permonitor for debugging or any other tool or debugging facilities are available ?

Thanks.

+1  A: 

You can use visual studio to set up breakpoints on certain threads. See here and here for how to do it.

George Stocker
+1  A: 

It depends what do you mean by "abnormal behavior"...

for most of the time, the visual studio debugger should be enough. the Threads and CallStack windows will give you a lot of information about what is going on.

for the heavy duty stuff you can use WinDbg+SOS. read about the !threads, !threadpool and !runaway commands.

Moshe Levi
Usually it would be more comfortable to use these commands via the Intermediate panel in VS (no need to launch WinDbg as a separate process..)
@opc : you are right as long as you don't need the other WinDbg commands. which is not always the case.
Moshe Levi
+1  A: 
Yassir
+1  A: 

If you have several threads of the same type* you could modify your code to only run one of each type of thread (or perhaps put it in the application's configuration file so you can change it quickly while debugging).

If the application still misbehaves then you know that it's an interaction between the different types of thread that's causing the problem. If it doesn't then it could be that there's some resource you've not thread locked properly (for example).

What I'm trying to say is simplify you application to the point where it's using the minimum number of threads to still be your original design.

* Not the best word to use, but for example if you spawn 10 threads to deal with file i/o only spawn 1.

ChrisF
Really good suggestion Mr Chris.
+1  A: 

How do you define abnormal behavior? Would that be an exception thrown? Not sure if this will help you but What I often do is name the thread object when I create it, then if I catch an exception or if certain criteria exists, I write to the event log. I include the time, the application name, the name of the thread and exception information. I don't just use it for debugging, I use it if a user complains about odd behavior or reports an error. Then I can go back and get information about it.

Jeremy
+1  A: 

As an alternative to debugging, you could do thread-related testing. The book The Art of Unit Testing has a section on this in Appendix B. The author mentions three tools (two of which he has a personal interest in):

TrueWill