gpt4 book ai didi

c++ - std::boost::asio::post/dispatch 使用哪个 io_context?

转载 作者:行者123 更新时间:2023-11-30 04:56:31 47 4
gpt4 key购买 nike

在使用 boost::asio 1.66 时,我在文档中读到 boost::asio::io_context::post已弃用 boost::asio::post , 与 boost::asio::io_context::dispatch 相同.因为在它们之前,io_context 的成员函数在哪里?之前,当然处理程序需要在某些 io_context 的上下文中执行即 executor我的问题是:

boost::asio::io_context::post 最简单的重载如何知道哪个 io_contextexecutor使用?

template< typename CompletionToken> DEDUCED post(CompletionToken && token); 的文档指出

Obtains the handler's associated executor object ex by performing get_associated_executor(handler).

但是get_associated_executor的文档我也不清楚。我的猜测是由于 Template argument deduction 它可以在当前执行的处理程序中以某种方式获取它,但我想确定,而且,如果我调用 post 是不够的在 boost::asio 处理程序之外。

最佳答案

文档的核心位于 associated_executor trait 中。 :

  • get()

    If T has a nested type executor_type, returns t.get_executor(). Otherwise returns ex.

  • executor_type

    If T has a nested type executor_type, T::executor_type. Otherwise Executor.

如果您的处理程序类型¹ 具有嵌套的 executor_type 类型,则假定调用 token.get() 将返回要使用的正确执行程序。

如果您在未指定执行程序/执行上下文的情况下传递一个 vanilla 可调用对象,您将获得一个默认构造的执行上下文实例:boost::asio::system_executor

这样做的目的是使用自定义处理程序类型实现 DoTheRightThing。例如。如果你在 strand 上发布一些东西,处理程序将被包装在特定于 strand 实现的类型中。 associated_executor 特征和同上 get_executor() 成员函数将协调以指向该链的执行程序。


¹ 或任何标记,以防您的调用模型不同,例如 yield 上下文

关于c++ - std::boost::asio::post/dispatch 使用哪个 io_context?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52458609/

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