Boost logo

Boost Users :

Subject: Re: [Boost-users] asio: async ops in multithreading environment
From: Marat Abrarov (abrarov_at_[hidden])
Date: 2013-03-06 14:17:31


> 1) One thread issues scatter-gather boost::asio::async_write() for
> boost::array<boost::asio::const_buffer, 2> on socket A.
> 2) Another thread closes socket A after that.
> 3) Meantime (simultaneously with 2), first buffer write is completed and
> one of the threads running io_service::run() starts to execute second part
> of async_write from 1).
>
> The problem is: second write which is issued internally by boost cannot be
> protected by the same mutex which is used for protected access to the
> socket in my code.

At first, RTFM:
1.
http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/Handler.h
tml
~~~~~~~~~~~~~~
asio_handler_invoke(f, &h);
~~~~~~~~~~~~~~

2.
http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/overview/core/stran
ds.html
~~~~~~~~~~~~~~
In the case of composed asynchronous operations, such as async_read() or
async_read_until(),
if a completion handler goes through a strand, then all intermediate
handlers should also go through the same strand.
This is needed to ensure thread safe access for any objects that are shared
between the caller and the
composed operation (in the case of async_read() it's the socket, which the
caller can close() to cancel the operation).
This is done by having hook functions for all intermediate handlers which
forward the calls to the customisable hook associated with the final
handler:
~~~~~~~~~~~~~~

Than google some more:
3.
https://github.com/boostcon/2011_presentations/raw/master/mon/thinking_async
hronously.pdf
See template <class Handler> struct mutex_wrapper at page 95.

4. http://asio-samples.blogspot.ru/2011/06/design-journeys-with-asio.html
Sorry, it's in Russian.

Regards,
Marat Abrarov.


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