Is it possible to call async_wait several times on the same boost::asio::deadline_timer?
What I mean to do is something like the following:
t->expires_from_now(delay);
t->async_wait(f1);
t->async_wait(f2);
Does this ensures that the two functions will be called? Does this ensures that the two functions will be called in this order?
If not, any idea how to have f1 and f2 successively called when the timer times out? (I don't care if another handler is executed between the calls to f1 and f2).
Another question: if two timers t1 and t2 are set such that the deadline of t1 is before the deadline of t2, can I be sure that the handler associated to t1 will be called before the handler associated to t2? (in which case for the above code, I would just create a second timer for f2 with a delay slightly larger than the delay set for the first timer).
Thanks
Carefully reading the documentation on http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/basic_deadline_timer/async_wait.html, it states that
For each call to
async_wait()
, the supplied handler will be called exactly once.
(emphasis mine). That means that in your case, both f1 and f2 will be called once.
To your second question: That depends on 3 conditions:
Update:
I just now realized that there was a second part in your first question regarding the order in which the handlers get called. The documentations does not say anything about that. You could look it up in the implementation, but that may change.
In the case that you want two functions executed in order, just call the second from the first one. If that second handler should get "appended" to the first one only in certain conditions, either delay the call to async_wait
until you know the whole extent of the handler chain, or just make them independent of each other.
The third possibility would be to roll your own, appendable handler. But bear in mind, that handlers get copied into the io_service::run
thread, i.e. into the async_wait
calls, so the appendable handler will need only a pointer to the real chain of handlers, it will need to take concurrency into account and so on.
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