问题
Does boost.asio provide any guarantees on completion handler ordering? I have initiated a single async_read & a single async_write operation. I am using the epoll_reactor internally. If the socket becomes both readable & writable simultaneously, will my read (or write) operation and hence completion handler be executed in a particular order always
Currently, reading the epoll_reactor.ipp:perform_io, that seems to be the case. But does the ASIO documentation guarantee that?
回答1:
There is no guarantee. Also, its better to not to rely on any such "ordering" things. This is big field for race conditions and other bugs.
回答2:
Boost.Asio provides no guarantees as to the order in which completion handlers will be invoked.
The io_service currently makes no guarantees about the invocation order of handlers. Thus, the io_service is free to choose an arbitrary order, even if underlying reactor implementation was known to perform operations and post completion handlers in a known order. Currently, only a strand specifies guarantees to the order in which posted handlers execute, and it explicitly notes that the order of completion handlers is unspecified.
Order of handler invocation
Given:
- a strand object
s- an object
ameeting completion handler requirements- an object
bmeeting completion handler requirements...
Note that in the following case:
async_op_1(..., s.wrap(a)); async_op_2(..., s.wrap(b));the completion of the first async operation will perform
s.dispatch(a), and the second will performs.dispatch(b), but the order in which those are performed is unspecified. That is, you cannot state whether one happens-before the other. Therefore [...] no ordering guarantee is made.
来源:https://stackoverflow.com/questions/37809895/boost-asio-async-read-async-write-completion-handler-ordering