1

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?

Yellow Ray
  • 221
  • 3
  • 9

1 Answers1

3

The question has been treated before, e.g.

It clearly spells out

if any of the following conditions are true:

  • s.post(a) happens-before s.post(b)
  • ...

then asio_handler_invoke(a1, &a1) happens-before asio_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 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.

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • ... 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