Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is sigwait() MT-safe but sigsuspend() is not?

I'm looking to write a multithreaded application where one thread waits for another to signal it before continuing. According to here, sigsuspend is not MT-safe because of a race condition. According to here, sigwait should be used in these cases. I'm trying to understand why.

Based on the man page descriptions (sigwait and sigsuspend), it appears that...

sigsuspend (const sigset_t *mask) actually changes the process's signal mask, which would affect all threads.

sigwait (const sigset_t *set, int *sig) just waits for one of the indicated signals in set without changing the thread's (or process's) signal mask.

Is this understanding correct? And if so, how does sigwait block without changing the signal mask?

like image 562
tonysdg Avatar asked Sep 27 '22 06:09

tonysdg


1 Answers

Actually sigwait changes the mask but then restore it, see this (Advanced Programming in the UNIX Environment): Signals and Threads.

And also this: Oracle Multithreaded Programming Guide

like image 137
terence hill Avatar answered Sep 30 '22 07:09

terence hill