Boost.asio: can I do async_read and async_write simultaneously from the same thread?

I read that several socket operations from different threads are not recommended. But what if I call socket.async_read and next socket.async_write from the same thread (without waiting for the end of the previous one)? Can I verify that the correct answer will be executed when one of these operations is completed?

+6
source share
1 answer

I found that yes, you can have one async_read pending and one incomplete async_write in the same socket without problems. When you call the io_service::run() method, the callbacks will complete as expected.

Issuing multiple async_reads in the same socket or multiple async_writes in the same socket may lead to unexpected behavior depending on the type of socket. In particular, using multiple async_writes on the same TCP socket can cause the data to work in a different order than you expected before, and mixing the data sends. In UDP, this might be more reasonable, but I would still recommend against it.

+11
source

All Articles