title: Boost.asio & UNIX signal handling
body: <h2>Preface</h2>
  
  <p>I have a multi-threaded application running via Boost.Asio. There is only one <code>boost::asio::io_service</code> for the whole application and all the things are done inside it by a group of threads. Sometimes it is needed to spawn child processes using fork and exec. When child terminates I need to make waitpid on it to check exit code an to collect zombie. I used recently added <code>boost::asio::signal_set</code> but encountered a problem under ancient systems with linux-2.4.* kernels (that are unfortunately still used by some customers). Under older linux kernels threads are actually a special cases of processes and therefore if a child was spawned by one thread, another thread is unable to wait for it using <code>waitpid</code> family of system calls. Asio's signal_set posts signal handler to <code>io_service</code> and any thread running this service can run this handler, which is inappropriate for my case. So I decided to handle signals in old good signal/sigaction way - all threads have the same handler that calls <code>waitpid</code>. So there is another problem:</p>
  
  <h2>The problem</h2>
  
  <p>When signal is caught by handler and process is successfully sigwaited, how can I "post" this to my <code>io_service</code> from the handler? As it seems to me, obvious <code>io_service::post()</code> method is impossible because it can deadlock on <code>io_service</code> internal mutexes if signal comes at wrong time. The only thing that came to my mind is to use some pipe or socketpair to write notifications there and <code>async_wait</code> on another end as it is done sometimes to handle signals in <code>poll()</code> event loops.</p>
  
  <p>Are there any better solutions?</p>