lotus



previous page: 9. (Unix) Are there any freely-available threads packages? (Programming)
  
page up: Threads Programming FAQ
  
next page: 11. (DCE?, POSIX) Why does everyone tell me to avoid asynchronous cancellation? (Programming threads)

10. (DCE, POSIX, UI) Why does my threaded program not handle signals sensibly? (Programming)




Description

This article is from the Threads Programming FAQ, by Bryan O'Sullivan bos@serpentine.com with numerous contributions by others.

10. (DCE, POSIX, UI) Why does my threaded program not handle signals sensibly? (Programming)

Signals and threads do not mix well. A lot of programmers start out by
writing their code under the mistaken assumption that they can set a
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.

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. Handling signals synchronously in
this way greatly simplifies things.

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 section on asynchronous
cancellation.

Finally, using sigwait and installing signals handlers for the signals
you are sigwaiting for is a bad idea.

 

Continue to:













TOP
previous page: 9. (Unix) Are there any freely-available threads packages? (Programming)
  
page up: Threads Programming FAQ
  
next page: 11. (DCE?, POSIX) Why does everyone tell me to avoid asynchronous cancellation? (Programming threads)