### Signal and Exception Handlers in Multithreaded Applications

Signal Handlers
When a thread starts, all of its signal handlers are set to SIG_DFL. If you want any other signal handling for a thread, you must explicitly register it by calling signal.

Handlers for synchronous signals are registered independently on each thread. These signals are always handled on the thread that generated them. For example, if the following statement is in thread 1, the handler handlerfunc is registered for thread 1 only and SIGFPE receives default handling on other threads:

>     signal (SIGFPE, handlerfunc);

The ascynhronous signals, SIGBREAK, SIGINT, and SIGTERM, are handled on the thread that generated them only if they are raised by the raise function. If they are generated by an exception, they are handled on thread 1. Therefore you must call the signal function on thread 1 to register a handler for these signals. If you explicitly raise an asynchronous signal, you must also register a handler on each thread that calls the raise function.

The ascynhronous signals, SIGBREAK, SIGINT, and SIGTERM, are handled on a new thread the operating system creates for the handler. This means other threads can be running at the same time as the handler, even if they link to the single-threaded run-time library. This may cause a problem because the single-threaded library is not serialized. Do not assume functions in the single threaded library are reentrant.

Exception Handlers
Using exception handlers can be especially helpful in multithread programs that use operating system semaphores. If you acquire a semaphore and then use longjmp either explicitly or through a signal handler to move to another place in your program, the semaphore is still owned by your code. Other threads in your program may not be able to obtain ownership of the semaphore.

If you register an exception handler for the function where the semaphore is requested, the handler can check for the unwind operation that occurs as a result of a longjmp call. If it encounters an unwind operation, it can then release the semaphore. Alternatively, you can take advantage of the fact that longjmp calls the destructors of automatic objects being removed from the stack, to unlock semaphores.

Default Exception Handler
IBM C and C++ Compilers automatically registers and deregisters the _Exception handler for each thread that you start by calling _beginthread. After it is registered, _Exception is the first exception handler to be called when an exception occurs.

If you use DosCreateThread to create a new thread, you must use #pragma handler to register the IBM C and C++ Compilers exception handler for the function that the new thread will run.

If you call CreateThread to create a new thread, you must use #pragma handler to register the IBM C and C++ Compilers exception handler for the function that the new thread will run.

Signals and Exceptions
Signal and Exception Handling
Multithreaded Applications

Terminate Execution with Multiple Threads

Default Exception Handling (OS/2)
Default Exception Handling (Windows)