There are one aspect of the timers in asynchronous connections I want to know if I understand correctly.
Suppose that we set a timer before performing a read operation, which includes a handler, and then, run()
the io_service.
As I have understood, the io_service ends as soon as the manager ends after being invoked, which can happen for two reasons:
a) the read operation is complete.
b) the timer has reached its limit.
Suppose that the first (a) condition has been reached, and the read operation has been completed before the timer ends.
The question is: What happens to that timer? Do we need to finish it. Say
dTimer_.expires_from_now (boost::posix_time::seconds(0));
after the io_service.run()?
Can you reset it to a new interval if necessary re-use the same timer object for another read operation?
Can I reset() the io_service and reuse the same object in a new run()
for that new operation?
The question is: What happens to that timer? Do we need to finish it.
The timer's handler will still be invoked if you do not cancel it
void my_read_handler() {
dTimer_.cancel(); // remember to catch exceptions
}
The async_wait handler
will be passed an error code of boost::asio::error::operation_aborted
if it was successfully canceled. If the async_wait
completed before the cancel
and the handler had already been queued by the io_service
, your handler will need to detect that condition and react appropriately.
Can you reset it to a new interval if necessary re-use the same timer object for another read operation?
A deadline_timer can be reset using expires_from_now
This function sets the expiry time. Any pending asynchronous wait operations will be cancelled. The handler for each cancelled operation will be invoked with the boost::asio::error::operation_aborted error code.
Can I reset() the io_service and reuse the same object in a new run() for that new operation?
The same io_service
object can be used again to run()
or poll()
after resetting it.
This function must be called prior to any second or later set of invocations of the run(), run_one(), poll() or poll_one() functions when a previous invocation of these functions returned due to the io_service being stopped or running out of work. This function allows the io_service to reset any internal state, such as a "stopped" flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With