boost::asio::deadline_timer precision issues

Hi,
I'm writing a timer class which calls a function every n ms. I'm using
Boost.Asio to achieve that. Please tell me if there is a better
solution. The usual pattern with boost::asio::deadline_timer is to call
`expires_from_now()' and `async_wait()' every time the timer expires.
The solution works, however the timer seems to have (important)
precision issues, as described in the sample code below. A simple
counter is incremented every 20 ms, and is displayed (and reset) every
1000 ms. The value displayed should therefore be close to 50. In
practice, it's closer to 40, which in unexplainable with such a simple
piece of code.
$ cat test.cc
#include

The solution works, however the timer seems to have (important) precision issues, as described in the sample code below. A simple counter is incremented every 20 ms, and is displayed (and reset) every 1000 ms. The value displayed should therefore be close to 50. In practice, it's closer to 40, which in unexplainable with such a simple piece of code.
The timer is ok, but pay attention that every time you call the
handler and then reset the timer, you accumulate an inaccuracy. The
1st timer fires much frequently than the 2nd, thus it accumulates much
more inaccuracy.
You can solve this problem by introducing a reference time and build on it.
Here's how your code should be modified:
#include
participants (2)
-
Igor R
-
Warren Seine