title: Catching signals such as SIGSEGV and SIGFPE in multithreaded program
body: <p>I am trying to write a multithreaded logging system for a program running on linux.</p>
  
  <p>Calls to the logging system in the main program threads pushes a data structure containing the data to be logged into a FIFO queue. A dedicated thread picks the data of the queue and outputs the data, while the programs main thread continues with its task.</p>
  
  <p>If the main program causes SIGSEGV or other signals to be raised I need to make sure that the queue is empty before terminating.</p>
  
  <p>My plan is to block the signals using pthread_sigmask <a href="http://man7.org/linux/man-pages/man3/pthread_sigmask.3.html" rel="nofollow noreferrer">http://man7.org/linux/man-pages/man3/pthread_sigmask.3.html</a> for all but one thread, but reading the list of signals on <a href="http://man7.org/linux/man-pages/man7/signal.7.html" rel="nofollow noreferrer">http://man7.org/linux/man-pages/man7/signal.7.html</a> i noticed:</p>
  
  <blockquote>
    <p><em>A signal may be generated (and thus pending) for a process as a whole (e.g., when sent >using kill(2)) or for a specific thread (e.g., certain signals, such as SIGSEGV and SIGFPE, >generated as a consequence of executing a specific machine-language instruction are
    thread directed, as are signals targeted at a specific thread using pthread_kill(3)).</em></p>
  </blockquote>
  
  <p>If I block SIGSEGV on all threads but a thread dedicated to catching signals, will it then catch a SIGSEGV raised by a different thread?</p>
  
  <p>I found the question <a href="https://stackoverflow.com/questions/11679568/signal-handling-with-multiple-threads-in-linux">Signal handling with multiple threads in Linux</a>, but I am clueless as to which signals are thread specific and how to catch them.</p>