Boost logo

Boost Users :

Subject: Re: [Boost-users] [Asio] Socket Read/Write Thread-Safety
From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2011-01-30 02:35:07


On Fri, Jan 28, 2011 at 1:48 PM, Timothy Liang <timothy003_at_[hidden]> wrote:
> Hi, I'm implementing a full-duplex protocol over TCP/IP using Boost.Asio.
> Multiple threads will be calling 'io_service::run_one()', so I have to
> serialize my completion handlers.  Since I need to be reading and writing on
> a socket at the same time, can I use the 'boost::asio::async_read(...)' and
> 'boost::asio::async_write(...)' functions?  The documentation states that
> these are implemented in terms of calls to the stream's async_read_some and
> async_write_some functions, and the 'basic_stream_socket' class isn't
> thread-safe.
>

I think you want multiple threads calling io_service::run instead of run_one.

To serialize your connection-related completion handlers you'd want to
wrap them in a Boost.Asio strand.

HTH

-- 
Dean Michael Berris
about.me/deanberris

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