I’ve read that multiple operations on sockets from different threads are not recommended. But what if I call from the same thread socket.async_read and next socket.async_write (without waiting for the previous to finish)? Can I expetct that proper callback will run when one of this operations is completed?
I’ve read that multiple operations on sockets from different threads are not recommended. But
Share
I’ve found that yes, you can have a single pending
async_readand a single pendingasync_writeon the same socket without an issue. When you call theio_service::run()method, the callbacks will complete as expected.Issuing multiple async_reads on the same socket, or multiple async_writes on the same socket, can result in unexpected behavior, depending on the type of socket involved. In particular, using multiple async_writes on the same TCP socket can result in data going out in a different order than you originally expected, and intermixing of data sends. In UDP, it might be more reasonable, but I would still recommend against it.