title: Segmentation Fault, on terminating a thread using pthread_exit()
body: <p>I have a client/server scenario. Client sends a message to server and immediately starts a thread that sleeps for 10 secs. While the main thread is waiting for any reply from server. If client gets any reply, within 10 sec, it will indicate the timer thread to terminate. The problem I am facing is that the thread don't terminate itself, everything else (communication, indication to thread) is fine. The code for this is:</p>
  
  <p>server:</p>
  
  <pre><code>if (recvfrom(conn_sock, buf, buff_length, 0, (struct sockaddr*)&amp;client_addr, &amp;slen) == -1)
      cout&lt;&lt;"ERROR: recvfrom()";
  
  cout&lt;&lt;"\nRECEIVED:\nClient : "
  &lt;&lt;"\nData : "&lt;&lt;buf;
  cout&lt;&lt;"\n\n";
  
  cout&lt;&lt;"\nEnter data to send(Type exit and press enter to exit) : ";
  cin.getline(buf, sizeof(buf), '\n');
  if(strcmp(buf,"exit") == 0)
      exit(0);
  
  if(sendto(conn_sock, buf, buff_length, 0, (struct sockaddr*)&amp;client_addr, slen) == -1)
      cout&lt;&lt;"ERROR: Problem sending data";
  </code></pre>
  
  <p>client:</p>
  
  <pre><code>cout&lt;&lt;"\nEnter data to send(Type exit and press enter to exit) :\n";
  cin.getline(buf, sizeof(buf), '\n');
  if(strcmp(buf,"exit") == 0)
      exit(0);
  
  if(sendto(conn_sock, buf, buff_length, 0, (struct sockaddr*)&amp;serv_addr, addr_len) == -1)
      cout&lt;&lt;"ERROR: Problem sending data";
  
  pthread_t thread_id;
  pthread_attr_t attr; // thread attribute
  pthread_attr_init(&amp;attr);
  pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_DETACHED);
  pthread_create(&amp;thread_id, &amp;attr, thread_timer_process, NULL);
  
  if(recvfrom(conn_sock, buf, buff_length, 0, (struct sockaddr*)&amp;serv_addr, &amp;slen) == -1)     cout&lt;&lt;"ERROR: recvfrom()";
  else
  termin = true;
  
  cout&lt;&lt;"\nRECEIVED:\nServer : "
  &lt;&lt;"\nData : "&lt;&lt;buf;
  cout&lt;&lt;"\n\n";
  </code></pre>
  
  <p>static variables and thread_timer_process():</p>
  
  <pre><code>static bool termin = false;
  static int times = 10;
  static const int INTERVAL_SEC = 1;
  static void* thread_timer_process(void*)
  {
      int i=0;
      cout&lt;&lt;"thread started\n";
      signal(SIGINT, signal_callback_handler);
      do
      {
          sleep(INTERVAL_SEC);
          cout&lt;&lt;"In thread, i : "&lt;&lt;i&lt;&lt;"\n";
          ++i;
      }
      while(i&lt;times &amp;&amp; termin == false);
  
      if(termin == true)
      {
          termin = false;
          cout&lt;&lt;"--is exiting-\n";
      }
      pthread_exit(NULL);
      cout&lt;&lt;"thread end\n";
  }
  </code></pre>
  
  <p>Is this the right way of doing what I am trying to do?</p>