[ASIO] Thread safety of socket.write_some & socket.read_some

Hi, I'm a beginner in using boost asio library (and coding c++ too :). I am currently trying to code a simple application that handles some communication with a server through TCP/IP connection. My I/O Class has two threads that use synchronous socket operations on the same socket (which is a member of the class) Main thread: CApplication::Close() - socket.close() CApplication::Open() - socket.open(boost::asio::ip::tcp::v4()); - socket.write_some(boost::asio::buffer(initial_command)); - socket.read_some(boost::asio::buffer(initial_command_reply)); CApplication::Write(data from another class) - m_Socket.write_some Workerthread: CApplication::thread_Run() do { - socket.read_some(boost::asio::buffer((void *)&character, 1)); - package bytes to messages and pass them on to another class } while (!bClosed) My question is that is it safe to call write_some and read_some from different threads without synchronization. The application is now working w/o problems but I need to be sure that this is a safe way to do this before continuing development. Thanks for your reply, James -- View this message in context: http://www.nabble.com/-ASIO--Thread-safety-of-socket.write_some---socket.rea... Sent from the Boost - Users mailing list archive at Nabble.com.

My question is that is it safe to call write_some and read_some from different threads without synchronization.
Accessing the same socket object from different threads is unsafe: http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/reference/ip__tcp/s... "Thread Safety Distinct objects: Safe. Shared objects: Unsafe. "

Igor R wrote:
Accessing the same socket object from different threads is unsafe:
http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/reference/ip__tcp/s... "Thread Safety Distinct objects: Safe. Shared objects: Unsafe. "
Hi Igor and thanks for your reply. I am sorry for not reading the documentation well enough :-) Does this mean that I can still use the socket object from different threads if I use some kind of locking method to make sure that only one thread at a time uses the socket? Thanks, James -- View this message in context: http://www.nabble.com/-ASIO--Thread-safety-of-socket.write_some---socket.rea... Sent from the Boost - Users mailing list archive at Nabble.com.

By the way, note that in the latest ASIO (boost 1.37.0) the synchronous operations are thread safe: http://beta.boost.org/users/news/version_1_37_0 Updated Libraries Asio: * Synchronous read, write, accept and connect operations are now thread safe (meaning that it is now permitted to perform concurrent synchronous operations on an individual socket, if supported by the OS).
Accessing the same socket object from different threads is unsafe:
http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/reference/ip__tcp/s... "Thread Safety Distinct objects: Safe. Shared objects: Unsafe. "
Does this mean that I can still use the socket object from different threads if I use some kind of locking method to make sure that only one thread at a time uses the socket?

Igor R wrote:
* Synchronous read, write, accept and connect operations are now thread safe (meaning that it is now permitted to perform concurrent synchronous operations on an individual socket, if supported by the OS).
That's great news. The only unsolved problem now is how to safely stop the synchronous read operation from another thread when I want to close the socket since close() or shutdown() do not seem to be thread safe yet (and as far as I know, cancel() can't be used on Windows XP). Thanks, James -- View this message in context: http://www.nabble.com/-ASIO--Thread-safety-of-socket.write_some---socket.rea... Sent from the Boost - Users mailing list archive at Nabble.com.

That's great news. The only unsolved problem now is how to safely stop the synchronous read operation from another thread when I want to close the socket since close() or shutdown() do not seem to be thread safe yet (and as far as I know, cancel() can't be used on Windows XP).
From my experience, it's worth getting used to the asynchronous i/o. It's much more flexible (in case you need cancellations, timeouts etc.), and with asio it is also quite convenient and simple. Besides, using the asynchronous paradigm might make some of i/o threading (and locking) unnecessary, thus simplifying the overall design.
participants (2)
-
Igor R
-
James12