Boost logo

Boost Users :

From: Gavin Lambert (boost_at_[hidden])
Date: 2019-09-09 01:31:34

On 4/09/2019 02:21, Stephan Menzel wrote:
> my experiments with custom composed operations in asio have led me to
> another question regarding the use of such methods.
> What do I do in order to have multiple handlers in flight, e.g. for a timer?

Not sure if this helps, but the way that I've done something similar in
the past is simply to have different overloads of operator() for each
unique operation signature.

(Note that my example is for a sequential operation, not a parallel one,
but in principle you should be able to do something similar.)

For example:

     -> kicks off first async_wait
     -> handles result of async_wait and kicks off async_read
   operator()(error_code, size_t)
     -> handles result of async_read and kicks off another async_wait

This doesn't actually use the coroutine mechanism since the actions are
split between multiple methods rather than being multiplexed, but I
think it still read pretty clearly this way.

If you're switching between two operations that have the same signature
(eg. async_read and async_write) then you'll need additional state in
the handler object to determine which one was in flight; if you're
trying to run them in parallel then you'll need to bind an extra handler
argument to disambiguate which one completed first.

Where your timer is being used as a timeout (so that you run them in
parallel) things get more complex, since you have to deal with
cancellation of the timer and/or the read (including coping with the
case when you requested cancellation but it actually completed
successfully before it could actually cancel; and not accidentally
interpreting acknowledgment of cancelling your previous timer wait as a
cancellation of a subsequent timer wait). But it should be doable.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at