What signals should not be blocked by a thread in a multithreaded program in C?

I have a multithreaded program. I want to process all significant signals in a single thread. This is what I do before creating any thread:

sigset_t signal_set, old_set; sigfillset(&signal_set); // What signals should I leave unblocked here? pthread_sigmask(SIG_SETMASK, &signal_set, &old_set); std::thread( /* some args */ ); pthread_sigmask(SIG_SETMASK, &old_set, nullptr); 

But my common sense suggests leaving some signals unlocked, as there are many situations where signals can be sent directly to a specific stream: SIGSEGV or SIGPROF - I think that it is as good as leaving an unlocked SIGINT in an interactive program.


Can my suggestions correct these two signals ( SIGSEGV , SIGPROF )?

What other signals should be left unlocked after some common sense?

+7
c multithreading signals
source share
1 answer

Asynchronous signals (most of them, including all transmitted by the kill command / function and signals generated by a control terminal of the SIGINT type), are delivered to any process thread in which the signal is unlocked, so you do not need to keep them unlocked in all threads. If you use a dedicated signal processing thread, you want them to be blocked in all threads except the signal processing thread.

Synchronous signals, on the other hand, are delivered to a particular stream as a result of the action of that stream. These include SIGPIPE , SIGBUS , SIGSEGV , SIGFPE , etc. With the exception of SIGPIPE , none of them should happen at all unless you have serious bugs in your program, and you probably want to block SIGPIPE anyway, so that you could get an EPIPE error EPIPE and handle this condition correctly. Therefore, for the most part, I would say that it does not hurt just to block them all. If you really need to handle SIGSEGV or such, you should probably rethink the reasons, but at the same time, feel free to unlock it.

+3
source share

All Articles