title: pthread: locking mutex with timeout
body: <p>I try to implement following logic (a kind of pseudo-code) using <code>pthread</code>:</p>
  
  <pre><code>pthread_mutex_t mutex;
  
  threadA()
  {
      lock(mutex);
      // do work
      timed_lock(mutex, current_abs_time + 1 minute);
  }
  
  threadB()
  {
      // do work in more than 1 minute
      unlock(mutex);
  }
  </code></pre>
  
  <p>I do expect <code>threadA</code> to do the work and wait untill <code>threadB</code> signals but not longer than 1 minute. I have done similar a lot of time in Win32 but stuck with pthreads: a <code>timed_lock</code> part returns imediately (not in 1 minute) with code <code>ETIMEDOUT</code>.</p>
  
  <p>Is there a simple way to implement the logic above?</p>
  
  <p>even following code returns <code>ETIMEDOUT</code> immediately</p>
  
  <pre><code>pthread_mutex_t m;
  // Thread A
  pthread_mutex_init(&amp;m, 0);
  pthread_mutex_lock(&amp;m);
  // Thread B
  struct timespec now;
  clock_gettime(CLOCK_MONOTONIC, &amp;now);
  struct timespec time = {now.tv_sec + 5, now.tv_nsec};
  pthread_mutex_timedlock(&amp;m, &amp;time); // immediately return ETIMEDOUT
  </code></pre>
  
  <p>Does anyone know why? I have also tried with <code>gettimeofday</code> function</p>
  
  <p>Thanks</p>