title: Create signal handler for a single thread
body: <p>I'm wondering if <code>sigaction</code> will create a signal handler for the calling thread or the whole process. If it unblocks a signal and creates a signal handler for the entire process, then how can I make sure only a single thread will use the signal handler and have other threads block the signal.</p>
  
  <p>In my case, I want a single thread to call a signal handler when an alarm signal goes off and the other threads to simply block it.</p>
  
  <hr>
  
  <pre><code>void alarmSigHandler(int signo){
  
  }
  
  void* alarm_thread_start_routine(void *arg){
  
      // Perform some tasks
  
  }
  
  
  int main(){
  
      // Main thread start
  
      /* Unblock alarm signal */
      /* Assign signal handler for alarm signal */
  
      /* Launch alarm signal handler thread */
  
      /* Block alarm signal with pthread_sigmask */
  
      /* Do something */
  
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p><a href="http://vip.cs.utsa.edu/classes/cs3733f2013/notes/SignalsAndThreads.html" rel="nofollow">http://vip.cs.utsa.edu/classes/cs3733f2013/notes/SignalsAndThreads.html</a></p>
  
  <p>If a signal is sent to a threaded program, any of the threads can handle the signal.</p>
  
  <p>Each thread inherits the process signal mask, but each thread has its own signal mask that can be modified with pthread_sigmask.</p>
  
  <p>sigprocmask should not be used in a threaded environment, but it can be used before the threads are created.</p>
  
  <p>The simplest way to handle signals in a multithreaded environment is to have a thread dedicated to signal handling.</p>
  
  <p>Issues involving signal safety can be handled by using sigwait:</p>
  
  <pre><code>The main process blocks all signals before creating any threads.
  No signal handlers are set up.
  A thread is created to handle the signals.
  That thread sets up a sigset_t containing the signals of interest.
  It loops, calling sigwait and handles the pending signals.
  </code></pre>