title: Precise way to reduce CPU usage in an infinite loop
body: <p>This is my code using <code>QueryPeformanceCounter</code> as timer.</p>
  
  <pre><code>//timer.h
  class timer {
  private:
      ...
  public:
      ...
      double get(); //returns elapsed time in seconds
      void start();
  };
  
  //a.cpp
  void loop() {
      timer t;
      double tick;
      double diff; //surplus seconds
      t.start();
      while( running ) {
          tick = t.get();
          if( tick &gt;= 1.0 - diff ) {
              t.start();
              //things that should be run exactly every second
              ...
          }
          Sleep( 880 );
      }
  }
  </code></pre>
  
  <p>Without <code>Sleep</code> this loop would go on indefinitely calling <code>t.get()</code> every time which causes high CPU usage. For that reason, I make it sleep for about 880 milliseconds so that it wouldn't call <code>t.get()</code> while not necessary.</p>
  
  <p>As I said above, I'm currently using <code>Sleep</code> to do the trick, but what I'm worried about is the accuracy of <code>Sleep</code>. I've read somewhere that the actual milliseconds the program pauses may vary - 20 to 50 ms - the reason I set the parameter to <code>880</code>. I want to reduce the CPU usage as much as possible; I want to, if possible, pause more than 990 milliseconds <strong>EDIT: and yet less than 1000 milliseconds</strong> between every loop. What would be the best way to go?</p>