Mixing threads and signals [Unix]

On Unix systems, using threads and signals together is a nightmare.

A lot of programmers start out by writing their code under the mistaken assumption that they can set a separate signal handler for each thread; this is not the way things work. You can block or unblock signals on a thread-by-thread basis, but this is not the same thing as having a separate handler per thread.

When it comes to dealing with signals, the best thing you can do is create a thread whose sole purpose is to handle signals for the entire process. This thread should loop calling sigwait(2); this allows it to deal with signals synchronously. You should also make sure that all threads (including the one that calls sigwait) have the signals you are interested in handling blocked. This is easiest to do by blocking these signals before ever creating the threads.

Note, also, that sending signals to other threads within your own process is not a friendly thing to do, unless you are careful with signal masks. For an explanation, see the FAQ entry on asynchronous cancellation.

Finally, don’t combine use of sigwait with installation of signal handlers for the signals you are sigwaiting for.

6 comments on “Mixing threads and signals [Unix]
  1. Josh says:

    That’s not entirely correct — the Linux clone() API which pthreads is built on top of can, in fact, allow separate signal handlers per thread. That’s actually dead useful in a lot of ways.

  2. Robin says:

    man pthread_sigmask

  3. Devyn says:

    DC,I think you should do a crtuqiie of the Pearce Piece that Abdreas kindly linked us to I just posted this at NewScientist: Fred,This piece, (like your gross misrepresentation of Mojib Latif’s speech at the third World Climate Conference in 2009), is riddled with errors. Not to mention clearly being sympathetic towards the skeptics and being uncritical of the nefarious actions of the people like Mosher, McIntyre and Curry. These people want the best of both worlds, they attack, slander and defame climate scientists and then at the same time expect to be treated with respect and claim to want to reconcile . This is nothing but PR campaign by the skeptics Fred, and I’m sad to see that it seems you have bought it hook-line and sinker.Some points, there were probably not 28 climate scientists at the conference. Do you consider Goddard a climate scientist? McIntyre is not a statistician by training. You misrepresented Dr. Gavin Schmidt’s position on attending the conference (go to Eli Rabett’s place for clarification) it seems that you did not solicit his opinion before writing this. The workshop was also financed by the Gulbenkian Foundation do some research on them Fred, they have ties to big oil.The scientific literature has shown again and again that the observed warming can not be explained by ENSO or PDO or other internal climate modes, because they simply move heat around in the system. Trenberth et al. (2002, JGR) showed that +0.06 C of the +0.4 C warming (about 15%) observed between 1950 and 1998 was attributable to trends in ENSO.These internal climate modes are internal drivers which can act to mute or enhance the underlying warming trend from higher CO2, they cannot and do not explain the fact that the planet is in a net positive energy imbalance (Murphy et al. 2009). How can the climate scientists you and the skeptics are chastising be indifferent to these internal climate modes and oscillations when they have published papers specifically to investigate their role? Please think about this the skeptics love to make unsubstantiated and unsupported claims, because they know they cannot back them up. What does count in this game of skeptics is rhetoric, innuendo and opinions, not facts.This is yet another astounding example of the media failing us. To say I am disappointed by your partisan and uncritical and error riddled piece would be a gross understatement.I would complain to your editors, but previous experience has shown me that NewScientist has no interest in acknowledging or retracting errors on the climate file, especially it seems when those errors are made by you.For what it is worth I urge you to please correct the errors pointed out to you here and to revise or retract your piece. Thank you.

  4. Courtney – I love biscotti, and I am always looking for new recipes to try–this one looks like a winner! Can’t wait to try it…I love the idea of the candied Clementine peels!Courtney

  5. Dear Evan and Michelle,Thank you so much for the pictures of the pre-scool Maya. She is so beautiful and I know she is as sweet as she is beautiful.We are thinking of you all as you go to Duke on Tuesday.We pray all goes well for the pre-treatment phase for Zoe. We hope you will be able to keep us up to date as you have been doing.Love to all, Nannie

Leave a Reply

Your email address will not be published. Required fields are marked *