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

时光怂恿深爱的人放手 提交于 2019-12-24 17:23:38

问题


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?


回答1:


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-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.



来源:https://stackoverflow.com/questions/31527192/serialize-io-servicepost-execution-with-io-servicerun-called-only-in-a-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!