Re: Am I using ThreadPool the right way?



Ricardo Vazquez wrote:
[...]
But, as I said, I'm new to .net and Visual Studio 2005...

What is it the way to debug a deadlock? Any guidelines, web links, orientation, detailed explanation...? :-)

First, you'll need to be using a retail version of Visual Studio. It is possible to debug deadlocks and other threading issues in the Express version, but it's not something I'd recommend for someone unfamiliar with the general techniques of dealing with thread issues in the first place, since Express doesn't provide any direct way to get at the individual threads in the debugger.

So, assuming you have a retail version of VS, the next step is to run your application under the debugger until it gets stuck (or attach the VS debugger to a process running your application after it gets stuck).

At that point, use the Break command to stop execution of your program. Once the process has been interrupted, you can then use the Threads drop-down in the Debug toolbar to examine the state of each thread. It will allow you to switch from one thread to another, with the source view, stack trace, local variables, etc. being updated according to the current thread.

Some of the thread will be irrelevant, and may not even include any of your own code (all of the code in the stack trace will be "external"). You'll be looking for threads that are stopped on a statement that waits for some resource, to identify which threads are involved in the deadlock and why they are waiting.

At some point, you should come across at least two threads that are waiting, each of which are waiting on a different resource, but in each case a resource being held by some other thread that is also waiting. That's your deadlock.

Once you've identified that, then you need to figure out what in your design led to the bug where you have that circular resource acquisition going on and fix that. Depending on your experience level that in and of itself could be a challenge, but regardless you need to first identify the problem for that to even be an option.

Pete
.



Relevant Pages

  • Re: How to programmatically avoid deadlock?
    ... There are four necessary conditions for deadlock to appear: ... Condition of waiting (threads hold some owned resource and wait to ... A non-blocking wait (where the thread can do other things while waiting) ...
    (comp.programming.threads)
  • Re: Threads and Deadlocks
    ... the deadlock because either it is holding a resource other threads are waiting on or it is waiting on a resource some other thread is using and sleeping. ... The deadlock is intentionally created in the most obvious way just to see what happened; in this sense the program is correct, it does exaclty what it was meant to: ... If I can somehow trap the event that makes the ruby interpreter terminate I could obviously recover the deadlock, since blocked resources would have been constructed to be recoverable. ...
    (comp.lang.ruby)
  • Re: detect deadlock
    ... E.g. your application may block on external input or two threads may have a deadlock while others are still running. ... other's resource, even I created a deadlock situation on purpose. ... You have to track the dependency tree of each waiting thread. ... Unfortunately it is not sufficient to track dependencies only of the threads of one process, because the dependency might involve resources of other programs or the system. ...
    (comp.programming.threads)
  • detect deadlock
    ... I use top to detect possible deadlocked process: ... top cannot show that the two threads are waiting for each ... other's resource, even I created a deadlock situation on purpose. ...
    (comp.programming.threads)
  • deadlock detection
    ... I use top to detect possible deadlocked process: ... top cannot show that the two threads are waiting for each ... other's resource, even I created a deadlock situation on purpose. ...
    (comp.os.linux.development.apps)