问题
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 ?
回答1:
According to the documentation of async_read(...)
This operation is implemented in terms of zero or more calls to the stream's
async_read_somefunction, and is known as a composed operation. The program must ensure that the stream performs no other read operations (such asasync_read, the stream'sasync_read_somefunction, 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.
来源:https://stackoverflow.com/questions/37098594/asio-multiple-pending-async-read