If I have io_service::run()
running only in a single thread, are io_service::post()
calls executed in the same order I request them to be executed, or they can be executed in arbitrary order and I still need to use strand
for forcing serialized execution?

- 221
- 3
- 9
1 Answers
The question has been treated before, e.g.
- Does boost::asio::io_service preserve the order of handlers??
- Documentation: Oreder Of Handler Invocations
It clearly spells out
if any of the following conditions are true:
s.post(a)
happens-befores.post(b)
- ...
then
asio_handler_invoke(a1, &a1)
happens-beforeasio_handler_invoke(b1, &b1)
.
Note that a single IO thread creates the implicit strand (docs)
Note In relation to the other answer: of course this doesn't hold when the handler invocations are done implicitly on completion of an asynchronous operation.
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 none of the above conditions are met and no ordering guarantee is made.
-
... but it is still guaranteed that invocation is not concurrent. (correct?) – Zero Sep 30 '15 at 01:06
-
@Zero only on an (implicit) strand. Which is, when you have only 1 thread `run()`-ing the service or explicitly serialized operations through a `strand` – sehe Sep 30 '15 at 01:30