4

async_read calls the callback when the requested amount of bytes have been received. This may imply multiple calls to async_read_some.

In the documentation it is specified that there can be at most one pending async_read_some. The callback has to be called before the next async_read_some may be issued.

What about the async_read ? Can multiple async_read be queued ? If yes, is the order of execution guaranteed to be preserved ?

Tanner Sansbury
  • 51,153
  • 9
  • 112
  • 169
chmike
  • 20,922
  • 21
  • 83
  • 106
  • 2
    `async_read` is a free function so it can be called multiple times to read data from different streams, NOT multiple times for the same stream. 'async_read_some` is a member function of a socket/stream which is why there can only be one pending `async_read_some`. I'm sure that someone with a better understanding of the inner workings of `asio` than me can explain precisely why... – kenba May 08 '16 at 12:12

1 Answers1

3

According to the documentation of async_read(...)

This operation is implemented in terms of zero or more calls to the stream's async_read_some function, and is known as a composed operation. 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.

That would imply that you can't start any further async_read operations on the same stream until the completion handler (the callback) is called -- this should be only once, when the provided buffer is filled, completion condition is satisfied, or some error occurs.

You're not entirely clear on what you mean by "queueing" multiple async_read operations. The way you would implement a sequence of async_read operations would be by starting the next operation in the completion handler.

Dan Mašek
  • 17,852
  • 6
  • 57
  • 85
  • 1
    Thanks. This answers my question. I meant calling async_read multiple times in the same handler for instance. – chmike May 13 '16 at 08:46