Boost logo

Boost Users :

Subject: Re: [Boost-users] [Asio] Socket Read/Write Thread-Safety (Timothy Liang)
From: Marat Abrarov (abrarov_at_[hidden])
Date: 2011-01-31 04:46:56


> The 'boost::asio::async_read(...)' and 'boost::asio::async_write(...)'
> functions might call async_read_some and async_write_some concurrently in
> some scenarios, as they do not synchronize accesses to the stream object
> that I'm passing to them as a reference. A look in their implementations
> confirms this: if the stream's async_read_some or async_write_some didn't
> satisfy the completion condition, it tries again, using the same reference
> to the stream object. With multiple threads calling
> io_service::run_one(), the result can be one thread calling
> async_read_some while another is calling async_write_some at the same
> time. This seems to prevent me from using async_read and async_write at
> all, so I'm wondering if the stream has some undocumented thread-safety
> guarantees, or if there's a proper way to achieve the same effect.

RTFM. Really, RTFM.
Use boost::asio::io_service::strand and don't worry about synchronization.

http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/overview/core/stran
ds.html

You can use async_read and async_write simultaneously (but note "Thread
Safety"
for used asio class) with strands easily. Yes, with multiple threads running
boost::asio::io_service::run on the same boost::asio::io_service object.

What amazing laziness in reading asio docs :) - all of the latest questions
about Boost.Asio have respective answers in documentation.

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