Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it thread safe to call async_send and async_receive at the same time?

I understood that calling boost::asio::ip::tcp::socket::async_receive (or boost::asio::ip::tcp::socket::async_send) two times may result in a bad behavior.. Is it OK if i call boost::asio::ip::tcp::socket::async_recive and boost::asio::ip::tcp::socket::async_send at the same time?

I am going to have 2 or more threads running the boost::asio::run so you need to take that into account..

Thanks

like image 465
grich Avatar asked Jan 20 '26 08:01

grich


1 Answers

This has to be OK. How else would you perform full duplex async communications on a single service? You need a receive outstanding at all times for incoming data.

The Boost docs indicate only that each of async_read and async_write must be called serially. For example, for async_read:

The program must ensure that the stream performs no other read operations (such as async_read, the stream's async_read_some function, or any other composed operations that perform reads) until this operation completes.

The docs for socket are not specific on this point, it's true.

like image 78
Steve Townsend Avatar answered Jan 22 '26 22:01

Steve Townsend