[boost.asio] Is it safe to call write() and async_read at the same time?

Is it safe to do the following in boost.asio? If it matters, I am using a serial port at the moment, but the code will be used for sockets as well. If not, what is the proper way to implement this to get full duplex communication? Thread 1: write(conn, buffer1) Thread 2: async_read(conn, buffer2)

Is it safe to do the following in boost.asio? If it matters, I am using a serial port at the moment, but the code will be used for sockets as well. If not, what is the proper way to implement this to get full duplex communication?
Thread 1: write(conn, buffer1)
Thread 2: async_read(conn, buffer2)
It is safe to read from a socket, when there's a pending write. It is not safe to access the socket object simultaniously from multiple threads.

On Wed, 11 May 2011 11:23:08 +0200, Igor R
Is it safe to do the following in boost.asio? If it matters, I am using a serial port at the moment, but the code will be used for sockets as well. If not, what is the proper way to implement this to get full duplex communication?
Thread 1: write(conn, buffer1)
Thread 2: async_read(conn, buffer2)
It is safe to read from a socket, when there's a pending write. It is not safe to access the socket object simultaniously from multiple threads.
Hmmm, are you sure? I've thought pthread sockets are thread-safe. Or do you mean boost layer over pthread sockets not being thread-safe? Anyway, regarding the original question I believe the asio sockets must be full-duplex, otherwise it'd be quite a step back over pthread implementation and IMO bug report worthy. -- Slava

Hmmm, are you sure? I've thought pthread sockets are thread-safe. Or do you mean boost layer over pthread sockets not being thread-safe?
I mean that ASIO documentation states explicitly: <<Thread Safety: Distinct objects: Safe. Shared objects: Unsafe>> http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/reference/ip__tcp/s...

On Thu, May 12, 2011 at 12:52 AM, Igor R
I mean that ASIO documentation states explicitly: <<Thread Safety: Distinct objects: Safe. Shared objects: Unsafe>> http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/reference/ip__tcp/s...
This is "the standard answer", but in my opinion, it's not good enough. The revision history for asio version 1.4.0 states the following: "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)." So clearly, the "Shared objects: Unsafe" does not tell the whole story. I also have yet to see an example where async_read/write's that could be could called simultaneously are surrounded by mutexes. It would be really nice to get a clear answer on whether these methods are thread safe or not.

So clearly, the "Shared objects: Unsafe" does not tell the whole story. I also have yet to see an example where async_read/write's that could be could called simultaneously are surrounded by mutexes. It would be really nice to get a clear answer on whether these methods are thread safe or not.
I absolutely agree. But as long as we don't have such answers, imho, it's worth using safer techniques to prevent unexpected behavior.
participants (3)
-
Frank
-
Igor R
-
Viatcheslav.Sysoltsev@h-d-gmbh.de