title: pthread_cancel() function failed to terminate a thread
body: <p>I'm using <code>pthread_create()</code> and <code>pthread_cancel()</code> functions to create a multithreaded program, but I noticed that <code>pthread_cancel()</code> did not really terminate the thread it was supposed to.</p>
  
  <pre><code>void check(void *param){
      header_t *Bag = (header_t *) param;
      pthread_t timer_thread;
      while(true){
          if(Bag-&gt;size &amp;&amp; TIMER_OFF){
              pthread_create(&amp;timer_thread, NULL, (void *) &amp;timer, (void *) Bag);
              printf("\nCREATE THREAD ID = %d\n", timer_thread);
              // ADD
          }
          else if(!TIMER_OFF &amp;&amp; Bag-&gt;size &gt;= 2 &amp;&amp; Bag-&gt;next-&gt;time &lt;= CURRENT_APT_TIME &amp;&amp; CRRENT_TAG != Bag-&gt;next-&gt;tag){
              printf("\nOLD THREAD ID = %d TO BE CANCELLED\n", timer_thread);
              pthread_cancel(timer_thread);
              pthread_create(&amp;timer_thread, NULL, (void *) &amp;timer, (void *) Bag);
              printf("\nNEW THREAD ID = %d\n", timer_thread);
              // Replace
          }
          Sleep(1);
      }
  }
  </code></pre>
  
  <p>timer function <code>void timer(void *)</code> is exactly what it sounds like, and I've included couple of lines to print out the thread ID of itself.</p>
  
  <p>When tested, the following was seen:</p>
  
  <pre><code>...
  
  OLD THREAD ID = 6041240 TO BE CANCELLED
  
  NEW THREAD ID = 6046456
  
  ...
  
  THREAD ID EXECUTING = 6041240
  </code></pre>
  
  <p>So the timer function was not terminated by calling <code>pthread_cancel()</code>?</p>