title: Finding difficulty using clock_nanosleep function to produce a sleep time of 2ms
body: <p>Thanks... I’m using the posix precision sleep standard lib function to sleep, but it appears to be returning far too quickly.  Please examine this code and provide details of a way which would repair the function to correctly sleep the correct amount of time.I  need a sleep time of atleast 2ms..                                                                                                                                                                             </p>
  
  <pre><code>void CModbusSlave::sleep(uint32_t seconds, uint32_t useconds)//arguments passed int32_t seconds=0,uint32_t useconds=2000ULL//
  {
      int timerResult = 0;
     clock_gettime(CLOCK_MONOTONIC, &amp;stopTime);
  
      uint64_t absTime = (((static_cast&lt;uint64_t&gt;(stopTime.tv_sec) * 1000000000ULL) +
                           (static_cast&lt;uint64_t&gt;(stopTime.tv_nsec))) +
                          ((static_cast&lt;uint64_t&gt;(seconds) * 1000000000ULL) +
                           (static_cast&lt;uint64_t&gt;(useconds)*1000ULL)));
      stopTime.tv_sec = static_cast&lt;uint32_t&gt;(absTime / 1000000000ULL);
      stopTime.tv_nsec = static_cast&lt;uint32_t&gt;(absTime % 1000000000ULL);
  
      do
      {
          timerResult = clock_nanosleep(CLOCK_MONOTONIC,
                                        TIMER_ABSTIME,
                                        &amp;stopTime,
                                        &amp;stopTime);
          sched_yield();
      }
      while ((timerResult == -1) &amp;&amp; (errno == EINTR));
  }
  </code></pre>