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#th... http://www.boost.org/doc/libs/1_63_0/doc/html/thread/synchronization.html#th... Good luck. Ben