Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost::asio] Wait for multiple async_read()'s
From: Ben Pope (benpope81_at_[hidden])
Date: 2017-01-21 09:11:06


On 21/01/17 12: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?
>
> One solution I've found, though it feels "un-asio" is to pass the
> use_future option as the hander for the reads. The result is futures
> for each read, which I can then place into a list<future>, and have an
> additional io_service.post()'ed handler call get() on these futures.
> The only issue with this is that I must call io_service.run() from
> more than one thread, since the handler calling get() blocks —
> otherwise I can potentially deadlock the whole application.

This is why futures without continuations aren't that useful; with
continuations, useful helpers can be made:

http://www.boost.org/doc/libs/1_63_0/doc/html/thread/synchronization.html#thread.synchronization.futures.reference.wait_for_all

http://www.boost.org/doc/libs/1_63_0/doc/html/thread/synchronization.html#thread.synchronization.futures.reference.when_all

Good luck.

Ben


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