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