Q:

Is it safe to assume that if I make two calls to async_read, the second call will be processed only after the first is processed?

I'm sending some data over the network and each data piece has a header consisting of two things:

data type
length of the rest of the data piece.

I'm reading the data type into one variable, and the length into another, thus the two async_read calls.
I know I can chain the async_reads and everything should work fine, i.e. first read the message type, and when it's read, in its async_read handler, call async_read for the data length.
But what if I don't do such chaining and just do async_read( data type ); async_read( data length );. Will Boost process the second async_read() only after the first has completed?
async_reads are called on the same tcp socket and on the same io_service object.

A:

No, this is not a safe assumption. async_read() is implemented as multiple calls to async_read_some(), so the reads can end up being interleaved.

