Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost::asio] Wait for multiple async_read()'s
From: Rutger ter Borg (rutger_at_[hidden])
Date: 2017-01-22 15:09:14


On 21-01-17 13:09, Adam Zegelin wrote:
> Hi,
>
> I'm writing a boost::asio application that communicates with multiple
> streams. I'd like to write data to these streams then wait for
> everyone to reply before performing computation on the resulting data.
>
> What is the best way to implement this?
>
> The route I've ventured down involves me calling async_write() on each
> stream, with the callback hander executing async_read(). But this
> where I'm stuck. What is the best way to execute one final hander once
> all read operations have completed?
>

[snip]

Not sure if it's the best way, but for this typical use case I've
written a combiner, which uses reference counting in a wrapped handler.
A part of this io_object's signature is

    combiner_service::wrapped_handler async_combine( const
boost::system::error_code& a1,
             const handler& handler );

and the following usage

   auto wrapped_handler = some_combiner_.async_combine(
default_error_code, ..handler when all is complete... )
   for( ..0 or more items.. ) {
       some_io_object.async_do_stuff( ..., wrapped_handler );
   }

in the example above, the operation will only finish if the variable
wrapped_handler goes out of scope, and all copies of wrapped_handler
have been called. The wrapped handler maximizes the final error code.

HtH,
Cheers,

Rutger


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