简体   繁体   中英

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

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 found that yes, you can have a single pending async_read and a single pending async_write on the same socket without an issue. When you call the io_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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM