title: How to put a thread to sleep for microseconds? (invalid implementation for sleep_for in MinGW)
body: <p>Since <code>std::chrono::sleep_for</code> accepts microseconds, I expected it to be able to sleep for microseconds with some reasonable bias (sleep <em>a little</em> longer, <em>definitely</em> not shorter).</p>
  <p>However, it turned out that I was wrong and it seems to be not possible with STL.
  MSVC implementation of <code>sleep_for</code> makes the thread to sleep for a few milliseconds (!!!).
  MinGW implementation bluntly ignores <code>sleep_for</code> command for microseconds, 1 millisecond, and have been noticed several times to wake up spuriously (though such behavior is not specified by the standard).</p>
  <p>When MinGW is told to sleep for 1 millisecond, it sleeps unreasonably longer than 1 millisecond or MSVC.</p>
  <p>Here is a code snippet that I use for testing</p>
  <pre><code>#include &lt;thread&gt;
  #include &lt;condition_variable&gt;
  #include &lt;chrono&gt;
  
  #include &lt;iostream&gt;
  #include &lt;locale&gt;
  
  struct separate_thousands : std::numpunct&lt;char&gt;
  {
      char_type do_thousands_sep() const override { return ','; }  // separate with commas
      string_type do_grouping() const override { return &quot;\3&quot;; } // groups of 3 digit
  };
  
  class StopWatch
  {
      std::chrono::steady_clock::time_point start_;
  
  public:
      StopWatch()
          : start_(std::chrono::steady_clock::now())
      {}
  
      void Reset()
      {
          start_ = std::chrono::steady_clock::now();
      }
  
      std::chrono::nanoseconds Result() const
      {
          return std::chrono::steady_clock::now() - start_;
      }
  
      template &lt;typename MetricPrefix&gt;
      std::chrono::duration&lt;long long, MetricPrefix&gt; Result(MetricPrefix) const
      {
          return std::chrono::duration_cast&lt;
              std::chrono::duration&lt;long long, MetricPrefix&gt;
          &gt;(Result());
      }
  
      template &lt;typename MetricPrefix&gt;
      operator std::chrono::duration&lt;long long, MetricPrefix&gt;() const
      {
          return Result(MetricPrefix());
      }
  };
  
  template &lt;typename DurationType&gt;
  std::chrono::nanoseconds SleepFor(DurationType duration)
  {
      StopWatch sw{};
      std::this_thread::sleep_for(duration);
      return sw;
  }
  
  template &lt;typename DurationType&gt;
  std::chrono::nanoseconds WaitFor(DurationType duration)
  {
      std::mutex cvMutex{};
      std::condition_variable cv{};
      StopWatch sw{};
  
      std::unique_lock&lt;std::mutex&gt; ul{cvMutex};
      cv.wait_for(ul, std::chrono::microseconds(1));
      return sw;
  }
  
  
  int main()
  {
  
      std::cout.imbue(std::locale(std::cin.getloc(), new separate_thousands()));
  
      StopWatch sw{};
      std::chrono::nanoseconds durationNanosec = sw;
      std::cout &lt;&lt; &quot;Duration between 2 \&quot;std::chrono::steady_clock::now()\&quot; calls: &quot; &lt;&lt;
          durationNanosec.count() &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;Duration of sleep_for 1 microsecond &quot;
          &lt;&lt; SleepFor(std::chrono::microseconds(1)).count()
          &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;Duration of sleep_for 5 microseconds &quot;
                &lt;&lt; SleepFor(std::chrono::microseconds(5)).count()
                &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;Duration of sleep_for 100 microseconds &quot;
                &lt;&lt; SleepFor(std::chrono::microseconds(100)).count()
                &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;Duration of sleep_for 500 microseconds &quot;
                &lt;&lt; SleepFor(std::chrono::microseconds(500)).count()
                &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;Duration of sleep_for 1 milliseconds &quot;
                &lt;&lt; SleepFor(std::chrono::milliseconds(1)).count()
                &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      // this executes far longer than 1 second on MinGW!
      for (size_t i = 0; i != 1000; ++i)
      {
          std::chrono::nanoseconds duration = SleepFor(std::chrono::milliseconds(1));
          if (duration &lt; std::chrono::milliseconds(1))
          {
              std::cout &lt;&lt; &quot;Slept less than 1ms : &quot; &lt;&lt; duration.count() &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
              break;
          }
      }
  
      // this executes far longer than 2 seconds on MinGW!
      for (size_t i = 0; i != 1000; ++i)
      {
          std::chrono::nanoseconds duration = SleepFor(std::chrono::milliseconds(2));
          if (duration &lt; std::chrono::milliseconds(2))
          {
              std::cout &lt;&lt; &quot;Slept less than 2ms : &quot; &lt;&lt; duration.count() &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
              break;
          }
      }
  
      std::cout &lt;&lt; &quot;Duration of sleep_for 10 milliseconds &quot;
                &lt;&lt; SleepFor(std::chrono::milliseconds(10)).count()
                &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;Duration of condition_variable::wait_for 1 microsecond &quot;
          &lt;&lt; WaitFor(std::chrono::microseconds(1)).count()
          &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;Duration of condition_variable::wait_for 5 microseconds &quot;
          &lt;&lt; WaitFor(std::chrono::microseconds(5)).count()
          &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;Duration of condition_variable::wait_for 1 millisecond &quot;
                &lt;&lt; WaitFor(std::chrono::milliseconds(1)).count()
                &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; &quot;Duration of condition_variable::wait_for 10 milliseconds &quot;
                &lt;&lt; WaitFor(std::chrono::milliseconds(10)).count()
                &lt;&lt; &quot; nanoseconds&quot; &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  <p>With MinGW the output may be something like this:</p>
  <pre><code>Duration between 2 &quot;std::chrono::steady_clock::now()&quot; calls: 100 nanoseconds
  Duration of sleep_for 1 microsecond 700 nanoseconds
  Duration of sleep_for 5 microseconds 300 nanoseconds
  Duration of sleep_for 100 microseconds 400 nanoseconds
  Duration of sleep_for 500 microseconds 200 nanoseconds
  Duration of sleep_for 1 milliseconds 7,956,100 nanoseconds
  Slept less than 2ms : 1,839,900 nanoseconds
  Duration of sleep_for 10 milliseconds 20,629,600 nanoseconds
  Duration of condition_variable::wait_for 1 microsecond 6,402,400 nanoseconds
  Duration of condition_variable::wait_for 5 microseconds 14,985,800 nanoseconds
  Duration of condition_variable::wait_for 1 millisecond 13,803,400 nanoseconds
  Duration of condition_variable::wait_for 10 milliseconds 14,900,900 nanoseconds
  </code></pre>
  <p>With MSVC:</p>
  <pre><code>Duration between 2 &quot;std::chrono::steady_clock::now()&quot; calls: 300 nanoseconds
  Duration of sleep_for 1 microsecond 13,200 nanoseconds
  Duration of sleep_for 5 microseconds 1,694,100 nanoseconds
  Duration of sleep_for 100 microseconds 1,572,900 nanoseconds
  Duration of sleep_for 500 microseconds 1,694,300 nanoseconds
  Duration of sleep_for 1 milliseconds 1,645,000 nanoseconds
  Duration of sleep_for 10 milliseconds 10,976,100 nanoseconds
  Duration of condition_variable::wait_for 1 microsecond 46,100 nanoseconds
  Duration of condition_variable::wait_for 5 microseconds 21,300 nanoseconds
  Duration of condition_variable::wait_for 1 millisecond 28,700 nanoseconds
  Duration of condition_variable::wait_for 10 milliseconds 22,500 nanoseconds
  </code></pre>
  <p>I am aware that I can use busy waiting to wait for nanoseconds, but this is a no go, since I do not want to waste CPU resources.
  Moreover, MinGW implementation seems to provide unpredictable results.</p>
  <ol>
  <li>Is there a way to sleep for microseconds?</li>
  <li>Is there a good crossplatform alternative for STL threads that implements sleeping for microseconds?</li>
  </ol>