title: pthread_cond_timedwait ignores cancellation request
body: <p>I've got a strange problem with pthread_cond_timedwait(): according to the POSIX specification it is a cancellation point. However, when I'm calling pthread_cancel() on the thread, it never gets cancelled! Instead, pthread_cond_timedwait() continues to run normally. It doesn't lock or anything, it just keeps running as if pthread_cancel() had never been called. As soon as I insert a pthread_testcancel() call, however, the thread gets cancelled correctly! Without the call to pthread_testcancel(), the thread is never cancelled although I'm calling pthread_cond_timedwait() all the time.</p>
  
  <p>Does anybody have an idea what's going wrong here? Thanks a lot!</p>
  
  <p>EDIT: Here comes the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;time.h&gt;
  #include &lt;sys/time.h&gt;
  
  // replacement function because OS X doesn't seem to have clock_gettime()
  static int clock_gettime(int clk_id, struct timespec* t)
  {
      struct timeval now;
      int rv = gettimeofday(&amp;now, NULL);
  
          if(rv) return rv;
  
      t-&gt;tv_sec = now.tv_sec;
          t-&gt;tv_nsec = now.tv_usec * 1000;
  
      return 0;
  }
  
  static void *threadproc(void *data)
  {
      pthread_mutex_t mutex;
      pthread_mutexattr_t attr;               
      pthread_cond_t cond;
  
      pthread_mutexattr_init(&amp;attr);
      pthread_mutexattr_settype(&amp;attr, PTHREAD_MUTEX_RECURSIVE);
  
      pthread_mutex_init(&amp;mutex, &amp;attr);      
      pthread_mutexattr_destroy(&amp;attr);
  
      pthread_cond_init(&amp;cond, NULL);
  
      for(;;) {
  
          struct timespec ts;
  
          clock_gettime(0, &amp;ts);
  
          // wait 60ms
          ts.tv_nsec += 60 * 1000000;
  
          pthread_mutex_lock(&amp;mutex);
          pthread_cond_timedwait(&amp;cond, &amp;mutex, &amp;ts);
          pthread_mutex_unlock(&amp;mutex);   
  
  #if 0
          pthread_testcancel();
  #endif      
      }
  
      return NULL;    
  }
  
  int main(int argc, char *argv[])
  {
      pthread_t pThread;
  
      pthread_create(&amp;pThread, NULL, threadproc, NULL);
  
      printf("Waiting...\n");
      sleep(5);
      printf("Killing thread...\n");
  
      pthread_cancel(pThread);
      pthread_join(pThread, NULL);
  
      printf("Ok!\n");
  
      return 0;
  }
  </code></pre>