falhumai falhumai - 4 months ago 12
Linux Question

Do I need A Different Sigset For Each C POSIX Thread To Ensure Atomicity When Masking Signals?

If the signals that I want to mask and unmask are common between all threads, can I use one global variable for the signal set in POSIX C to pass to pthread_sigmask, or there should be different sigsets for each thread?

Answer

The second argument of pthread_sigmask() is constant (const sigset_t *set) [meaning that the memory pointed by set will not be modified], so you can declare a single [possibly global, at your opinion] variable, without the need of implementing any thread locking mechanism while accessing it as it won't be modified. All threads will always read the same [consistent] value from it.

You can declare something like:

/* Global scope */
sigset_t g_set;

And pass it to pthread_sigmask():

/* Assuming `g_set` is already properly set at this point */
pthread_sigmask(how, &g_set, NULL);