I would like to implement simple network protocol. The message is being sent through the socket and the reply is received within timeout milliseconds. The question arises here is how to set order between timeout handler and async_read_until handler. Unfortunately, strands won't help here much:
http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/reference/io_service__strand.html
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 perform s.dispatch(b), but the order in which those are performed is unspecified. That is, you cannot state whether one happens-before the other. Therefore none of the above conditions are met and no ordering guarantee is made.
Imagine that io_service io
is shared between a number of different handlers, operations and so on. Then, between time moments A and B io.run()
will run some handler function which is not relevant to this particular socket operation at all. Then actual data may be received by operating system at time moment C which is A < C < B.
Also, the timer may be expired at time moment D which is A < D < B. Two this events: data reception C and timer expiration D will be detected when io.run()
will finish with the current handler job not earlier than time moment B.
What I want is to preserve handler invocation order, i.e. that async_read handler is called before async_wait handler if and only if C < D. Otherwise I could end up with the situation when the data were actually obtained before timeout deadline but discarded as timeouted.