Boost logo

Boost Users :

Subject: Re: [Boost-users] [Asio] Socket Read/Write Thread-Safety
From: Timothy Liang (timothy003_at_[hidden])
Date: 2011-01-29 22:49:09


> Your statements are correct, but I'm not sure I got the question. If you
> ask whether you may call async_read and async_write simultaneously on the
> same socket, then the answer is yes, of course.

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.


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