title: pthread_cond_timedwait returns error 138
body: <p>I can't find any information on this with Google, so I post here hoping that someone can help...</p>
  
  <p>My problem is with the Windows pthread function <code>pthread_cond_timedwait()</code>. When the indicated time is elapsed, the function should return with value ETIMEDOUT. Instead in my code, where its conditional variable is not signaled, it returns the value 138 and does it much earlier than the expected timeout, sometimes immediately.</p>
  
  <p>So my questions are: what is this error 138? And why is the timeout not completely elapsed?
  The code I use for the thread is:</p>
  
  <pre><code>int retcode = 0;
  timeb tb;
  ftime(&amp;tb);
  struct timespec timeout;
  timeout.tv_sec = tb.time + 8;
  timeout.tv_nsec = tb.millitm * 1000 * 1000;
  
  pthread_mutex_lock(&amp;mutex_);
  retcode = pthread_cond_timedwait(&amp;cond_, &amp;mutex_, &amp;timeout);
  pthread_mutex_unlock(&amp;mutex_);
  if (retcode == ETIMEDOUT)
    {
    addLog("Timed-out. Sending request...", LOG_DEBUG);
    }
  else // Something happened
    {
    std::stringstream ss;
    ss &lt;&lt; "Thread interrupted (Error " &lt;&lt; retcode &lt;&lt; ")";
    addLog(ss.str().c_str(), LOG_DEBUG);
    }
  </code></pre>
  
  <p>Is there something wrong with my absolute timeout computation?</p>
  
  <p>Only this thread and the calling thread are present. The calling one joins the created one just after its creation and correctly waits until it finishes.
  Currently the conditional variable <code>cond_</code> is never signaled, but if I try to do it, the <code>pthread_cond_timedwait()</code> returns with value 0 as expected.
  Even if not shown here, both <code>cond_</code> and <code>mutex_</code> are correctly initialised (if I dont't do it, I get a EINVAL error).</p>
  
  <p>Also following the pthread code I can't find this error. I can only find some <code>return errno</code> that could produce it, but I don't know the meaning of the 138.</p>
  
  <p>If it can help, I am using Visual Studio 2003 with pthreads win32 v2.9.1.</p>
  
  <p>Thanks,</p>
  
  <p>RG</p>