title: Simple Detached pthread does not cancel! (cout blocks and interleaves even if mutexed)
body: <p>I have a hard  problem here, which I can not solve and do not find the right answer on the net:</p>
  
  <p>I have created a detached thread with a clean up routing, the problem is that on my Imac and Ubuntu 9.1 (Dual Core). I am not able to correctly cancel the detached thread in the fallowing code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;pthread.h&gt;
  #include &lt;sched.h&gt;
  #include &lt;signal.h&gt;
  
  #include &lt;time.h&gt;
  
  pthread_mutex_t mutex_t;
  
  using namespace std;
  
  static void cleanup(void *arg){
      pthread_mutex_lock(&amp;mutex_t);
      cout &lt;&lt; " doing clean up"&lt;&lt;endl;
      pthread_mutex_unlock(&amp;mutex_t);
  }
  
  
  static void *thread(void *aArgument)
  {
  
      pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
      pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
  
      pthread_cleanup_push(&amp;cleanup,NULL);
      int n=0;
      while(1){
          pthread_testcancel();
          sched_yield();
          n++;
  
          pthread_mutex_lock(&amp;mutex_t);
          cout &lt;&lt; " Thread 2: "&lt;&lt; n&lt;&lt;endl; // IF I remove this endl;  --&gt; IT WORKS!!??
          pthread_mutex_unlock(&amp;mutex_t);
  
      }
      pthread_cleanup_pop(0);
  
      return NULL;
  }
  
  
  int main()
  {
  
      pthread_t thread_id;
  
      pthread_attr_t attr;
      pthread_attr_init(&amp;attr);
      pthread_attr_setdetachstate(&amp;attr,PTHREAD_CREATE_DETACHED);
  
      int error;
  
      if (pthread_mutex_init(&amp;mutex_t,NULL) != 0) return 1;
  
      if (pthread_create(&amp;thread_id, &amp;attr, &amp;(thread) , NULL) != 0) return 1;
  
      pthread_mutex_lock(&amp;mutex_t);
      cout &lt;&lt; "waiting 1s for thread...\n" &lt;&lt;endl;
      pthread_mutex_unlock(&amp;mutex_t);
  
      int n =0;
  
      while(n&lt;1E3){
          pthread_testcancel();
          sched_yield();
          n++;
  
          pthread_mutex_lock(&amp;mutex_t);
          cout &lt;&lt; " Thread 1: "&lt;&lt; n&lt;&lt;endl;
          pthread_mutex_unlock(&amp;mutex_t);
      }
  
      pthread_mutex_lock(&amp;mutex_t);
      cout &lt;&lt; "canceling thread...\n" &lt;&lt;endl;
      pthread_mutex_unlock(&amp;mutex_t);
  
      if (pthread_cancel(thread_id) == 0)
      {
          //This doesn't wait for the thread to exit
          pthread_mutex_lock(&amp;mutex_t);
          cout &lt;&lt; "detaching thread...\n"&lt;&lt;endl;
          pthread_mutex_unlock(&amp;mutex_t);
  
          pthread_detach(thread_id);
  
          while (pthread_kill(thread_id,0)==0)
          {
                  sched_yield();
          }
  
          pthread_mutex_lock(&amp;mutex_t);
          cout &lt;&lt; "thread is canceled";
          pthread_mutex_unlock(&amp;mutex_t);
  
      }
  
      pthread_mutex_lock(&amp;mutex_t);
      cout &lt;&lt; "exit"&lt;&lt;endl;
      pthread_mutex_unlock(&amp;mutex_t);
  
      return 0;
  }
  </code></pre>
  
  <p>When I replace the Cout with printf() i workes to the end "exit" , but with the cout (even locked) the executable hangs after outputting  "detaching thread...</p>
  
  <p>It would be very cool to know from a Pro, what the problem here is?.
  Why does this not work even when cout is locked by a mutex!?</p>