简体   繁体   English

Boost.asio:我可以从一个线程同时执行async_read和async_write吗?

[英]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)? 但是,如果我从同一个线程socket.async_read和下一个socket.async_write调用(不等待前一个完成)怎么办? 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. 我发现是的,你可以在同一个套接字上有一个挂起的async_read和一个挂起的async_write而没有问题。 When you call the io_service::run() method, the callbacks will complete as expected. 当您调用io_service::run()方法时,回调将按预期完成。

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. 在同一个套接字上发出多个async_reads,或在同一个套接字上发出多个async_writes,可能会导致意外行为,具体取决于所涉及的套接字类型。 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. 特别是,在同一个TCP套接字上使用多个async_writes会导致数据以与您原先预期不同的顺序发出,并且数据发送的混合。 In UDP, it might be more reasonable, but I would still recommend against it. 在UDP中,它可能更合理,但我仍然建议反对它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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