How to know that a thread in a Thread Pool hangs/freezes?

I don't want to be too presumptuous here, but a good dose of actually finding out what the problem is and fixing it is the best course with deadlocks.

I don't want to be too presumptuous here, but a good dose of actually finding out what the problem is and fixing it is the best course with deadlocks. Run a debug version of your service and wait until it deadlocks. It will stay deadlocked as this is a wonderful property of deadlocks.

Attach the Visual Studio debugger to the service. "Break All". Bring up your threads windows, and start spelunking... Unless you have a sound architecture\design\reason to choose victims in the first place, don't do it - period.It's pretty much a recipe for disaster to arbitrarily bash threads over the head when they're in the middle of something.

1 That's not too presumptuous at all. The correct response to "How do I re-attach my head to my body after I've cut it off? " is actually "Don't cut it off in the first place, ya goose!"

:-) – paxdiablo Dec 14 '10 at 8:06 1 @paxdiablo I was keen not to outline any sort of deadlock detection algorithm as that would involve some sort of thread-safety to implement, and well, you know. Fighting fire with petrol :) – chibacity Dec 14 '10 at 8:13.

One way is to use a watchdog timer (a solution usually done in hardware but applicable to software as well). Have each thread set a thread-specific value to 1 at least once every five seconds (for example). Then your watchdog timer wakes every ten seconds (again, this is an example figure only) and checks to ensure that all the values are 1.

If they're not 1, then a thread has locked up. The watchdog timer then sets them all to 0 and goes back to sleep for the next cycle. Providing your worker threads are written in such a way so that they will be able to set the values in a timely manner under non-frozen conditions, this scheme will work okay.

The first thread that locks up will not set its value to 1, and this will be detected by the watchdog timer on the next cycle. However, a better solution is to find out why the threads are freezing in the first place and fix that.

(This is perhaps a bit lowlevel, but at least it is a simple solution. As I don't know C#'s API, this is a general solution for any language using thread-pools. ) Insert a watchdog task after each real task that updates a time value with the current time.

If this value is larger than you max task run time (say 10 seconds), you know that something is stuck. Instead of setting a time and polling it, you could continuously set and reset some timers 10 secs into the future. When it triggers, a task has hung.

The best way is probably to wrap each task in a "Watchdog" Task class that does this automatically. That way, upon completion, you'd clear the timer, and you could also set a per-task timeout, which might be useful. You obviously need one time/timer object for each thread in the threadpool, but that's solvable via thread-local variables.

Note that this solution does not require you to modify your tasks' code. It only modifies the code putting tasks into the pool.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions