Hey,

On 21 January 2017 at 13:09, Adam Zegelin <adam@zegelin.com> wrote:
Hi,


Another alternative I can think of is that the function that schedules
all the async_writes knows ahead-of-time the number of reads that need
to be completed. Each async_read() handler would put its results into
a shared list and then, if the list's size is equal to the expected
number of results, fires the "completion" handler. While easy enough
to implement, this seems fraught with issues. If there is more than
one thread calling io_service.run(), then access to the shared list
has to be synchronised. Also, it doesn't seem very generic — I have a
number of places where this write n, read n, execute (or is it map,
reduce?) pattern needs to happen, and I'd prefer a solution that was
write one, worked everywhere.

​I would definitely go with this approach​. It's by far the simplest and to me it seems to match an asynchronous philosophy pretty well.

You can use an io_service::strand [1] to synchronize access to the shared list easily without explicit locking. For best performance you should probably keep the handler running in the strand as short as possible and keep the parts of the read handler that don't access shared state outside of the strand.

[1] http://www.boost.org/doc/libs/1_63_0/doc/html/boost_asio/reference.html#boost_asio.reference.io_service__strand

​Hope ​this helps,

-- Maarten