Jae Park
Jae Park

Reputation: 641

How to monitor Unexpectedly exited Threads?

In multi thread programming, what if one of worker thread is unexpectedly exited and main thread needs to know whether that thread is alive or not.

Is there any way to check this?

I was wondering if there is a typical signal that is made when worker thread is exited.

(Linux)

Thank you

Upvotes: 2

Views: 1600

Answers (1)

Kaz
Kaz

Reputation: 58666

If threads are unexpectedly dying in your program, it is toast. If you want fault isolation, with recovery, use multiple processes (with shared memory) instead of, or inaddition to threads. On POSIX (and Win32 also) you can detect if the owner of a process-shared mutex died while holding that mutex and implement some "fsck-like" check and repair of the shared data to try to restore its invariants. (Obviously it helps you if the data structure is designed with recoverable transactions in mind.)

On Win32 you can use Windows structured exception handling (SEH) to catch any kind of exception in a thread. (For instance access violation, division by zero, ...). Using the tool help API you can gain a list of the attached modules, and there are interfaces for reading the machine registers, faulting address, etc.

In POSIX you can do that with signal handling. Events like access violations and such deliver signals to the thread to which they pertain.

It doesn't seem realistic to code up these pieces into a recovery strategy that tries to keep a buggy program running.

Upvotes: 2

Related Questions