Zaghp
Zaghp

Reputation: 23

Thread and system signals

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:

Upvotes: 0

Views: 192

Answers (2)

iabdalkader
iabdalkader

Reputation: 17312

Since you asked about POSIX, from man signal(7)

POSIX.1 distinguishes the notions of signals that are directed to the process as a whole and signals that are directed to individual threads. According to POSIX.1, a process-directed signal (sent using kill(2), for example) should be handled by a single, arbitrarily selected thread within the process.

So in short, it means a random thread will be chosen to handle the signal.

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 477120

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.

Upvotes: 5

Related Questions