title: pthread_cond_timedwait hanging with gdb
body: <p>I'm using <code>pthread_cond_timedwait</code> on a thread loop to execute at every X ms (unless it is waked first).</p>
  
  <p>When I'm using gdb to debug it sometimes it the function never returns. </p>
  
  <p><a href="http://linux.derkeiler.com/Newsgroups/comp.os.linux.development.apps/2008-06/msg00117.html" rel="nofollow" title="this forum post">This forum post</a> also have the same problem, but there is no solution.</p>
  
  <p>Here's some code that reproduces the problem:</p>
  
  <pre><code>#include &lt;errno.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  static pthread_cond_t s_cond = PTHREAD_COND_INITIALIZER;
  static pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
  
  int main(int argc, char **argv)
  {
      int rc = 0;
      struct timespec curts = { 0 }; /* transformed timeout value */
  
      clock_gettime(CLOCK_REALTIME, &amp;curts);
      curts.tv_sec += 10; /* Add 10 seconds to current time*/
  
      pthread_mutex_lock(&amp;s_mutex);
  
      printf("pthread_cond_timedwait\n");
      rc = pthread_cond_timedwait(&amp;s_cond, &amp;s_mutex, &amp;curts);
      if (rc == ETIMEDOUT)
      {
          printf("Timer expired \n");
      }
  
      pthread_mutex_unlock(&amp;s_mutex);
  
      return 1;
  }
  </code></pre>
  
  <p>If I run it, it will run OK, and if I run in gdb it will also run OK.</p>
  
  <p>I've narrowed down to these steps (I've named the program <code>timedTest</code> ):</p>
  
  <ol>
  <li><p>Run the program;</p></li>
  <li><p>While it runs attach gdb to it;</p></li>
  <li><p>Execute <code>continue</code> on gdb;</p></li>
  <li><p>The <code>timedTest</code> program never returns...;</p></li>
  </ol>
  
  <p>Then, if I hit <code>Ctrl+C</code> on the terminal running gdb and run <code>continue</code> again, then the program will return.</p>
  
  <p>I can probably use some other method to achieve what I want in this case, but I assume that it should be a solution to this problem.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Looks like this only happens in some machines, so maybe there's something to do with gcc / glibc / gdb / kernel versions...</p>
  
  <p>Versions where this happens almost always:</p>
  
  <pre><code>$ ldd --version
  ldd (Ubuntu EGLIBC 2.13-0ubuntu13) 2.13
  
  $ gcc --version
  gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
  
  $ gdb --version
  GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
  
  $ uname -a
  Linux geovani 2.6.38-8-generic-pae #42-Ubuntu SMP Mon Apr 11 05:17:09 UTC 2011 i686 i686 i386 GNU/Linux
  </code></pre>