Yellow Ray
Yellow Ray

Reputation: 241

Serialize io_service::post() execution with io_service::run() called only in a single thread

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?

Upvotes: 1

Views: 359

Answers (1)

sehe
sehe

Reputation: 392954

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.

Upvotes: 3

Related Questions