title: POSIX pthread programming
body: <p>I have to code a multithreaded(say 2 threads) program where each of these threads do a different task. Also, these threads must keep running infinitely in the background once started. Here is what I have  done. Can somebody please give me some feedback if the method is good and if you see some problems. Also, I would like to know how to shut the threads in a systematic way once I terminate the execution say with Ctrl+C.</p>
  
  <p>The main function creates two threads and let them run infinitely as below.</p>
  
  <p>Here is the skeleton:</p>
  
  <pre><code>void    *func1();
  void    *func2();
  
  int main(int argc, char *argv[])
  {   
  
      pthread_t th1,th2;
      pthread_create(&amp;th1, NULL, func1, NULL);
      pthread_create(&amp;th2, NULL, func2, NULL);
  
      fflush (stdout);
      for(;;){
      }
      exit(0); //never reached
  }
  
  void *func1()
  {
      while(1){
      //do something
      }
  }
  
  void *func2()
  {
      while(1){
      //do something
      }
  } 
  </code></pre>
  
  <p>Thanks.</p>
  
  <p><strong>Edited code using inputs from the answers:</strong>
  Am I exiting the threads properly?</p>
  
  <pre><code>#include &lt;stdlib.h&gt;     /*  exit() */
  #include &lt;stdio.h&gt;      /* standard in and output*/
  #include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;time.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;semaphore.h&gt;
  
  sem_t end;
  
  void    *func1();
  void    *func2();
  
  void ThreadTermHandler(int signo){
      if (signo == SIGINT) {
          printf("Ctrl+C detected !!! \n");
          sem_post(&amp;end);
          }
  }
  void *func1()
  {
      int value;
      for(;;){
          sem_getvalue(&amp;end, &amp;value);
          while(!value){
              printf("in thread 1 \n");
          }
      }
      return 0;
  }
  
  void *func2()
  {
      int value;
      for(;;){
          sem_getvalue(&amp;end, &amp;value);
          while(!value){
              printf("value = %d\n", value);
          }
      }
      return 0;
  }
  
  int main(int argc, char *argv[])
  {
  
  
      sem_init(&amp;end, 0, 0);
      pthread_t th1,th2;
      int value  = -2;
      pthread_create(&amp;th1, NULL, func1, NULL);
      pthread_create(&amp;th2, NULL, func2, NULL);
  
      struct sigaction sa;
      sigemptyset(&amp;sa.sa_mask);
      sa.sa_flags = SA_SIGINFO;
      sa.sa_sigaction = ThreadTermHandler;
      // Establish a handler to catch CTRL+c and use it for exiting.
      if (sigaction(SIGINT, &amp;sa, NULL) == -1) {
          perror("sigaction for Thread Termination failed");
          exit( EXIT_FAILURE );
      }
  
      /* Wait for SIGINT. */
      while (sem_wait(&amp;end)!=0){}
      //{
          printf("Terminating Threads.. \n");
          sem_post(&amp;end);
                  sem_getvalue(&amp;end, &amp;value);
          /* SIGINT received, cancel threads. */
          pthread_cancel(th1);
          pthread_cancel(th2);
          /* Join threads. */
          pthread_join(th1, NULL);
          pthread_join(th2, NULL);
      //}
      exit(0);
  }
  </code></pre>