Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 484
xingzhi.sg Avatar asked Nov 01 '22 10:11

xingzhi.sg


1 Answers

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.

like image 76
Tanner Sansbury Avatar answered Nov 15 '22 03:11

Tanner Sansbury