gpt4 book ai didi

c++ - 使用仅在单个线程中调用的 io_service::run() 序列化 io_service::post() 执行

转载 作者:行者123 更新时间:2023-11-30 02:37:32 26 4
gpt4 key购买 nike

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

最佳答案

这个问题之前已经处理过,例如

写的很清楚

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

请注意,单个 IO 线程会创建隐式链(文档)


注意 关于另一个答案:当处理程序调用在异步操作完成时隐式完成时,当然这不成立。

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.

关于c++ - 使用仅在单个线程中调用的 io_service::run() 序列化 io_service::post() 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31527192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com