title: clock_nanosleep not working?
body: <p>I try to sleep for half a second with this snippet of code using clock_nanosleep, but i can't get it to work. My code always executes immediately, as if the sleep wasn't there. What am I doing wrong?</p>
  
  <pre><code>    while (true) 
  {
  
      /* CRITICAL SECTION BEGIN */
      pthread_mutex_lock(&amp;lock);
  
      ret = clock_gettime(CLOCK_MONOTONIC, &amp;now);
      if (ret) {
          perror ("clock_gettime");
          exit(1);
      }
  
      for (i = 0; i &lt; some_value ; i++){
          /* Do some stuff */
  
      }
      /* Update sleep time */
      now.tv_nsec += 500000000;
  
      pthread_mutex_unlock(&amp;lock);
      /* CRITICAL SECTION END */
  
      clock_gettime(CLOCK_MONOTONIC, &amp;debug);
      printf("Clockgoal - now: : %lu\n", now.tv_nsec - debug.tv_nsec);
      clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &amp;now, NULL);
  
  }
  </code></pre>
  
  <p>This is running as a pthread but it doesn't sleep at all. The printf output shows like this:</p>
  
  <pre><code>Done Updating Time!
  Clockgoal - now: : 499997443
  Done Updating Time!
  Clockgoal - now: : 499997441
  </code></pre>
  
  <p>What am I doing wrong? (Do I need some specific kernel config to allow for this kind of sleep? I'm running on a pretty minimal gentoo, so maybe I have to check some configuration option?</p>
  
  <p>In general, what I want to achieve is that this thread does some updates to some values, and then sleeps for half a second, INCLUDING the time it was working. If I'm doing it wrong, and there is a better alternative, please tell me, as I'm not bound to use clock_nanosleep for this, it just seemed to be the appropriate solution.</p>