lukecam95
lukecam95

Reputation: 77

Why do I need to use Pthread_sigmask in multithreaded programs to assure that a signal is being handled by the set handler?

I have a multi-threaded program which needs to handle the Linux signal SIGVTALRM sent by a setitimer() every 25ms. However I am confused. I do not know why I need to use the Pthread_sigmask() to block and unblock the signal. Won't the signal be handled anyway when it is sent, regardless of which thread is processing at the given time instant?

Upvotes: 1

Views: 1208

Answers (1)

P.P
P.P

Reputation: 121397

Won't the signal be handled anyway when it is sent, regardless of which thread is processing at the given time instant?

In a single threaded program, yes. But in a multi-threaded program, POSIX doesn't specify which thread would receive the signal SIGVTALRM you send. Hence, pthread_sigmask() is typically used to block the interested signals and handle fetch those signal(s) sigwait() in a dedicated thread. This is probably the reason why you are using or asked to use pthread_sigmask().

The linked POSIX manual also provides a simple example showing how this can be done.

Upvotes: 2

Related Questions