Reputation: 475
Looking at the documentation it looks like the TCP socket object is not thread-safe. So I cannot issue async_read from one thread and async_write concurrently from another thread? Also I would guess it applies to boost::asio::write() as well?
Can I issue write() - synchronous, while I do async_read from another thread? If that is not safe, then only way is probably to get the socket native handle and use synchronous linux mechanisms to achieve concurrent read and writes. I have an application where the reads and writes are actually independent.
Upvotes: 3
Views: 1531
Reputation: 10406
It is thread-safe for the use-cases you listed. You can read in one thread, and write in another. And you can use the synchronous as well as asynchronous operations for that.
You will however run into problems, if you try to do one dedicated operation type (e.g. reads) from more than one thread. Especially if you are using the freestanding/composed operations (boost::asio::read(socket)
instead of socket.read_some()
. The reason for this is one the primitive operations are atomic / threadsafe. And the composed operations are working by calling multiple times into the primitives.
Upvotes: 4