views:

314

answers:

4

Hi, Is it possible to detect a hung thread? This thread is not part of any thread pool, its just a system thread. Since thread is hung, it may not process any events.

Thanks,

+3  A: 

In theory, it is impossible. If you are on Windows and suspect that the thread might be deadlocked, I guess you could use GetThreadContext a few times and check if it is always the same, but I don't know how reliable it will be.

erikkallen
+1 for being impossible
rmeador
The proposed solution will only catch very few of the possibilities that a thread might appear "hung". It might catch deadlocks, but it will not catch livelocks.
erikkallen
+1  A: 

Not in theory, but in practice it may be possible, depending on your workload. For example if it is supposed to respond to events, you could post a thread message (in windows) and see if it responds. You could set an event or flag that would cause it to do something - you then have to wait for a "reasonable" amount of time to see if it has responded. The question then arises what you would do with the "hung" thread, even if it has really hung and isn't just taking a long time to respond. The thread cannot generally safely be killed and you cannot generally interrupt an arbitrary thread. It is safe enough to log a message to the effect, but who will care? Probably the best thing to do is to note it and figure out the bug that is causing it to hang.

1800 INFORMATION
A: 

Depending on the workload and the kinds of processing done and other details, it may be possible to detect a hung thread. In some cases, modern VMs can detect a lock deadlock where two threads are hung waiting for the other to release a lock. (But don't rely on this, because it isn't always possible, only sometimes.)

We need a lot more information before we can give a specific answer to your question.

Eddie
+1  A: 

Position one mirror behind you and one in front of you. Stand slightly to the side of that mirrors so that you can see the reflection of the mirror behind you on the mirror in front of you.

This setup will allow you to look at your sweater or slacks and detect that pesky hung thread.

Once you've found the thread, but sure not to pull on it! Doing so could unravel your entire sweater or put an "run" in your slacks.

Dan