Boost logo

Boost Users :

Subject: [Boost-users] How to reuse system_timer of boost::asio with async_wait?
From: Bruce (liu.bl_at_[hidden])
Date: 2017-01-03 02:47:38


Hi,
I am doing a program that use asio to construct a timer thread. The thread use asio to async_wait many timers. These timers are constructed by boost::asio::system_timer. These timers are mostly designed to fire only once. When they are fired or canceled, I put them to a free queue to reuse.

But when I reuse these times, I called expire_from_now() and async_wait() again on the timer, I got the timer fired immediately with error_code set to "ec == boost::asio::error::operation_aborted". And the timer are not fired after like 5 seconds late as I set to expire_from_now(seconds(5)).

How to reuse the timer correctly?

My codes basically are as follows:

m_timer.cancel(); // I tried to cancel the timer first, but no use, I still get the timer fired at once
m_timer.expires_from_now(std::chrono::seconds(nInterval));
m_timer.async_wait(boost::bind(&qmInnerTimer::Fire, this, _1));


Thanks.
-Bruce



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net