Reputation: 12259
My question is just what the title says (for Unix/Linux only). I assume that user calls to std::raise
and std::abort
are always executed in the same thread that made the call, but, I have some questions about asynchronous and kernel routines sending signals...
malloc
detects some heap corruption, will the signal be sent to the main thread or will be executed as well in the thread that called malloc
? The same questions applies to bugs generating SIGFPEs or SIGSEGVs.Upvotes: 1
Views: 1010
Reputation: 58534
The main()
thread is not special. It receives signals like any other thread.
When a signal is generated "asynchronously" for a process or process group (e.g., by kill
or a Ctrl-C at the terminal), the implementation may choose any thread for delivery.
The thread executing main()
is no different from any other thread in the process. If you want to control which thread receives the signal, you must block signal delivery in all but one thread, or block and call sigwait
from your desired thread.
When a signal is generated "synchronously" within a thread (as by raise
, abort
, a segmentation fault, etc.) or is targeted at a specific thread (as by pthread_kill
), then that thread, and only that thread, will get the signal. If that thread is blocking the signal, then the signal is held pending until unblocked or accepted.
Two very good sources here are the comprehensive but comparatively dense POSIX treatment and the pleasantly accessible GNU libc treatment of UNIX signals. Both are oriented for C rather than C++, however.
Upvotes: 3