I’m programing a multithreaded program that needs to intercept system signals (such as SIGINT). I would like to know if there is a normalized way to “catch” these signals like:
- A Signal is sent, may any thread receive it or only the main() one ?
- Is there a Posix rule or a programming idiom that specifies how to handle this ?
It is guaranteed that precisely one thread receives the signal, but it is also unspecified which thread that is.
The proper thing to do is to block signals on all threads but one, so that that thread alone deals with signal handling; or on Linux specifically block threads everywhere and set up a signalfd to catch signals — that way, you don’t introduce any asynchronousity and signals become just one more file descriptor to read from.