简体   繁体   中英

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

如果我只在一个线程中运行io_service::run()io_service::post()调用是按照我请求它们执行的相同顺序执行的,或者它们可以按任意顺序执行,我仍然需要使用用于强制序列化执行的strand

The question has been treated before, eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM