title: Am I over-engineering per-thread signal blocking?
body: <p>In my applications, I generally want to intercept <code>SIGINT</code> and <code>SIGTERM</code> signals in order to close down gracefully.</p>
  
  <p>In order to prevent worker threads from "stealing" signals, I do this in the entrypoint for each:</p>
  
  <pre><code>// Block signals in this thread
  sigset_t signal_set;
  sigaddset(&amp;signal_set, SIGINT);
  sigaddset(&amp;signal_set, SIGTERM);
  sigaddset(&amp;signal_set, SIGHUP);
  sigaddset(&amp;signal_set, SIGPIPE);
  pthread_sigmask(SIG_BLOCK, &amp;signal_set, NULL);
  </code></pre>
  
  <p>If I don't, when I perform <kbd>Ctrl</kbd>+<kbd>C</kbd>, some of the time (it's unspecified as to which thread will get the signal) my handlers in the base thread won't be invoked &mdash; instead, the signal just terminates the process from within the worker thread. This is obviously not cool.</p>
  
  <p>So I have one signal-handling thread and block signals everywhere else.</p>
  
  <p>However, I don't notice anybody else doing this, it's easy enough to forget to do, and it's also not entirely portable. Is there some more simple trick I'm missing?</p>
  
  <hr>
  
  <p>References:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2575106/posix-threads-and-signals">POSIX threads and signals</a></li>
  <li><a href="http://www.linuxprogrammingblog.com/all-about-linux-signals?page=11" rel="nofollow noreferrer">http://www.linuxprogrammingblog.com/all-about-linux-signals?page=11</a></li>
  <li><a href="http://www.unixguide.net/network/socketfaq/2.19.shtml" rel="nofollow noreferrer">http://www.unixguide.net/network/socketfaq/2.19.shtml</a></li>
  </ul>