Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] multiple simultaneous async_read calls
From: Bjorn Reese (breese_at_[hidden])
Date: 2014-08-08 07:31:09


On 08/08/2014 03:54 AM, bbxiong wrote:

> Will asio guarantee len1 bytes received by the first async_read, then
> schedule async_read2?

No such guarantees are given.

> or the result is undefined(like several bytes filled into buffer1, then
> next several bytes into buffer, then next several bytes into buffer1)?

Yes, this may happen.

Asio is opportunistic in the sense that it will try to complete an
asynchronous operation immediately if it does not block. However, if
the TCP stack does not contain all the requested data, then the first
async_read() will read as much as it can, and then be scheduled for
further reads. Unfortunately, the second async_read() is invoked
inbetween, and if more data has become available in the TCP stack,
then it will opportunisticly read that data, before being scheduled
for further reads.

> if the result is undefined, why asio don't add a check here?

Performance.

If you always want to read two buffers at the time, you may consider
using scatter I/O.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net