简体   繁体   中英

Concurrent read and async_read_some in boost asio

Assume that an async_read_some service has been enabled on a socket in boost::asio, what will happen if a blocking read on the same socket is called?

A piece of pseudo code looks like:

using boost::asio::local::stream_protocol;

boost::asio::io_service io;

stream_protocol::socket s(io);
s.connect(stream_protocol::endpoint(address));

s.async_read_some(aBuffer, aCallback);  // start async_read

boost::thread thread(boost::bind(&boost::asio::io_service::run, &io));

usleep(1000000); // do some stuff    

boost::asio::read(bBuffer);  // request a blocking read

My naive test shows that the blocking read always get priority : data will first fill the bBuffer before the async callback being called. It's a desired behaviour on my side.

Question: It is a guaranteed behaviour? On all socket types?

Boost.Asio does not make this guarantee on any I/O object. When a synchronous operation is initiated on an I/O object that has an outstanding asynchronous operation of the same type, the order in which the underlying system calls occur is unspecified.

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