I have a process with multiple threads running. If one thread sends a signal to the process, will every thread see the signal? If not which thread will? Are signals thread-safe?
post #1 of 3
5/28/08 at 7:18pm
Well seeing as no-one else seems to be answering, I'll give it a go... I'm kinda learning this stuff myself at the moment in my (limited) spare time but here's what I think I've understood so far (picking stuff up from various sites).
If anyone who knows this stuff thinks I've made a mistake please let me know... always happy to learn I'm assuming you're using Unix / Linux or BSD/MacOS here - Windows has a completely different (and more simple) model.
First rule for implementing signal handlers: don't make any assumptions about which thread is handling the signal.
In a single-threaded application, all signal handlers run on the main thread. In a multithreaded application, signals that are not tied to a specific hardware error (such as an illegal instruction) get delivered to whichever thread happens to be running at the time.
If multiple threads are running simultaneously, the signal is delivered to whichever one the system happens to pick - it's completely arbitrary. i.e. signals can be delivered to any active thread of your application.
If a specific thread wants to handle a given signal, you need to work out some way of notifying that thread when the signal arrives. You cannot assume that installation of a signal handler from that thread will result in the signal being delivered to the same thread.
If you only want some threads to be able to handle the signal, and you want to disable it for the rest, you can use pthread_sigmask(). If you need it to be delivered to a specific worker thread, all the other threads must be masked. To disable the handling of a signal altogether, you can use sigprocmask().
No... I changed my mind - the first rule of signal handling is that signals and multiple threads are a bad combo and should be avoided like the plague
Anyway hope that was some help and you're not more confused than me now.
Incidentally, I just found this