Sure, it needs its own stack and register set. But what thread context it's running in matters too. For one thing, that controls what work gets interrupted while the signal is processed.
For example, suppose thread A is holding a recursive lock and has changed some critical structure to an inconsistent state. That's fine though, because it won't release the lock until it returns those structures to a consistent state.
Now, say the signal interrupts thread A, and the signal handler goes to acquire the lock to check that critical structure. It gets the lock, because it's the thread that already holds the lock and the lock is recursive. Since it just got the lock that protects the critical structure, it expects to be able to access it and find it in an consistent state. But, of course, it's in a inconsistent state. Boom! You just crashed.
So in this case, the signal handler must always go to a thread that never acquires that lock. That way, when the signal handler acquires the lock, it is sure that no other code holds that lock and thus the structure must be in a consistent state.
It is common in multi-threaded code to have one thread whose sole purpose is to catch all external signals sent to the program. It handles the signals one-by-one, so the signal never interrupts code that might hold a lock.
Thank you for answering this question, too :-) So you better not choose the wrong thread? All the more does carrying out a signal in the context of a thread sound like a curse rather than a bless. Why choose a context at all? – artistoex – 2011-12-05T20:48:30.753
Well, if you need to acquire locks, you need a thread context in which to acquire them because that's how the locks are designed. As I said, a simple and common solution is to have one thread just to accept external signals, and you accept them synchronously and process them one at a time from a context known to be safe. All your other threads can then block those signals. The UNIX signal mechanism predates multi-threading, and they don't fit as nicely as they could. – David Schwartz – 2011-12-05T21:09:44.487
So I should read about locks in detail to understand this. Thank you! – artistoex – 2011-12-07T09:40:40.670