title: C++ How to make timer accurate in Linux
body: <p>Consider this code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;functional&gt;
  #include &lt;map&gt;
  #include &lt;atomic&gt;
  #include &lt;memory&gt;
  #include &lt;chrono&gt;
  #include &lt;thread&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/asio/high_resolution_timer.hpp&gt;
  
  static const uint32_t FREQUENCY = 5000; // Hz
  static const uint32_t MKSEC_IN_SEC = 1000000;
  
  std::chrono::microseconds timeout(MKSEC_IN_SEC / FREQUENCY);
  boost::asio::io_service ioservice;
  boost::asio::high_resolution_timer timer(ioservice);
  
  static std::chrono::system_clock::time_point lastCallTime = std::chrono::high_resolution_clock::now();
  static uint64_t deviationSum = 0;
  static uint64_t deviationMin = 100000000;
  static uint64_t deviationMax = 0;
  static uint32_t counter = 0;
  
  void timerCallback(const boost::system::error_code &amp;err) {
    auto actualTimeout = std::chrono::high_resolution_clock::now() - lastCallTime;
    std::chrono::microseconds actualTimeoutMkSec = std::chrono::duration_cast&lt;std::chrono::microseconds&gt;(actualTimeout);
    long timeoutDeviation = actualTimeoutMkSec.count() - timeout.count();
    deviationSum += abs(timeoutDeviation);
    if(abs(timeoutDeviation) &gt; deviationMax) {
      deviationMax = abs(timeoutDeviation);
    } else if(abs(timeoutDeviation) &lt; deviationMin) {
      deviationMin = abs(timeoutDeviation);
    }
  
    ++counter;
    //std::cout &lt;&lt; "Actual timeout: " &lt;&lt; actualTimeoutMkSec.count() &lt;&lt; "\t\tDeviation: " &lt;&lt; timeoutDeviation &lt;&lt; "\t\tCounter: " &lt;&lt; counter &lt;&lt; std::endl;
  
    timer.expires_from_now(timeout);
    timer.async_wait(timerCallback);
    lastCallTime = std::chrono::high_resolution_clock::now();
  }
  
  using namespace std::chrono_literals;
  
  int main() {
    std::cout &lt;&lt; "Frequency: " &lt;&lt; FREQUENCY &lt;&lt; " Hz" &lt;&lt; std::endl;
    std::cout &lt;&lt; "Callback should be called each: " &lt;&lt; timeout.count() &lt;&lt; " mkSec" &lt;&lt; std::endl;
    std::cout &lt;&lt; std::endl;
  
    ioservice.reset();
    timer.expires_from_now(timeout);
    timer.async_wait(timerCallback);
    lastCallTime = std::chrono::high_resolution_clock::now();
    auto thread = new std::thread([&amp;] { ioservice.run(); });
    std::this_thread::sleep_for(1s);
  
    std::cout &lt;&lt; std::endl &lt;&lt; "Messages posted: " &lt;&lt; counter &lt;&lt; std::endl;
    std::cout &lt;&lt; "Frequency deviation: " &lt;&lt; FREQUENCY - counter &lt;&lt; std::endl;
    std::cout &lt;&lt; "Min timeout deviation: " &lt;&lt; deviationMin &lt;&lt; std::endl;
    std::cout &lt;&lt; "Max timeout deviation: " &lt;&lt; deviationMax &lt;&lt; std::endl;
    std::cout &lt;&lt; "Avg timeout deviation: " &lt;&lt; deviationSum / counter &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>It runs timer to call timerCallback(..) periodically with specified frequency. In this example, callback must be called 5000 times per second. One can play with frequency and see that actual (measured) frequency of calls is different from desired one. In fact the higher is the frequency, the higher is deviation. I did some measurements with different frequencies and here is summary:
  <a href="https://docs.google.com/spreadsheets/d/1SQtg2slNv-9VPdgS0RD4yKRnyDK1ijKrjVz7BBMSg24/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1SQtg2slNv-9VPdgS0RD4yKRnyDK1ijKrjVz7BBMSg24/edit?usp=sharing</a></p>
  
  <p>When desired frequency is 10000Hz, system miss 10% (~ 1000) of calls.
  When desired frequency is 100000Hz, system miss 40% (~ 40000) of calls.</p>
  
  <p><strong>Question: Is it possible to achieve better accuracy in Linux \ C ++ environment? How?</strong> I need it to work without significant deviation with frequency of 500000Hz</p>
  
  <p>P.S. My first idea was that it is the body of the timerCallback(..) method itself causes delay. I measured it. It takes a stably takes less than 1 microsecond to execute. So it does not affect the process.</p>