Is it possible to freeze all threads when one thread encounters an exception

Go To StackoverFlow.com

1

I am able to print a stack trace of a thread which has caught an exception at runtime. I also need to print a trace of other threads running within the same process. I'm interested in finding a good way to freeze all threads as soon as the exception is caught in one thread.

For now, all i can do is query for each active thread then SuspendThread() on it. However, this will result in each thread executing a bit longer after the initial exception is caught. Is there a way to IMMEDIATELY cause every thread to suspend?

Thanks.

2012-04-05 02:46
by glutz
Kill the process with a crash report? Any sort of signal you send to another thread will take time. I guess the real question is for your purposes, what qualifies as instantaneous and how do you get a signal there that fast - Nick McCormack 2012-04-05 02:50
I don't think "immediately" would be possible in general, even for the OS... for example, on a multicore/multiprocessor machine, how would core #2 know to stop its thread when core #1's thread crashed? Core #1 would have to send a "suspect thread" message over to core #2, by which time core #2's thread would have already executed some more instructions - Jeremy Friesner 2012-04-05 03:02
Depending on how your application is architectured, you might be able to use fibers instead of threads. (Of course, you wouldn't want to do this in the release version, but it might be OK for debugging. - Harry Johnston 2012-04-05 03:46
Threads are unsynchronized by default. It doesn't make sense to talk about suspending the other threads "immediately" since they're operating on different timelines. Only at synchronization events is there a notion of "same time", "before" and "after - MSalters 2012-04-05 07:29
+1 for Jeremy - it can't be done, even with OS support, for exactly the reason described - the interprocessor driver has to interrupt the core/s running other threads and this does not happen 'immediately' - Martin James 2012-04-05 09:00


2

Did you try to use PostMessage() to GUI thread from the worker thread which was caught exception?

Brief step as proposed:
1) GUI thread spawn few worker threads
2) worker thread PostMessage() once caught exception
3) GUI thread receive message
4) GUI thread instruct remaining worker thread to suspend

Note: define your own message

By this way, you will still see some delay before suspending your worker threads.

Alternative way is to use Synchronization Object like event handle among worker threads.

2012-04-05 03:52
by wengseng
Ads