title: pthreads: How to handle signals in a main thread that creates other threads? (specific code shown)
body: <p>I have a main thread, which stays in the main function, i.e. I do not create it specifically as in pthread_create, because it's not necessary. This thread opens a file, then creates other threads, waits for them to finish their work (i.e., does the join), cleans up everything (pointers, semaphores, conditional variables and so on...).</p>
  
  <p>Now, I have to apply this code to block SIGINT:</p>
  
  <pre><code>sigset_t set; 
  int sig; 
  sigemptyset(&amp;set); 
  sigaddset(&amp;set, SIGINT); 
  pthread_sigmask(SIG_BLOCK, &amp;set, NULL); 
  
  while (1) { 
          sigwait(&amp;set, &amp;sig); 
          switch (sig) { 
                  case SIGINT: 
                          /* handle interrupts */ 
                          break; 
                  default: 
                          /* unexpected signal */ 
                          pthread_exit((void *)-1); 
          } 
  }
  </code></pre>
  
  <p>and it says <em>You must use the  main() function to launch the N+1 threads and wait for their completion. If a SIGINT signal arrives at the program it should be handled by the main thread in order to shutdown the program and its threads a clean way</em></p>
  
  <p>My doubt is how should I put this code? Is it wrong to put it on a background thread created in main() ? Because I already have a cicle, with an exit flag, that creates and join all the other threads, so I don't understand if this code goes exactly to the main function where all is done/called to initiate the program. If I put it on a thread, with this code and the handler to clean, is this considerated as busy waiting?</p>