asio::strandlt;asio::io_context::executor_typegt; vs io_context::strand(Asio::strandlt;asio::io_context::executor_typegt;VS io_CONTEXT::Strand)
问题描述
自Boost的最新版本以来,ASIO推出了新的执行器,并提供了asio::strand<Executor>
。因此,现在完全可以使用asio::strand<asio::io_context::executor_type>
而不是io_context::strand
。但它们不能互换使用。
- 谁能解释一下和举例的用法有什么区别吗?
- 他们的优势/不方便?
推荐答案
io_context::strand
是旧版(&Q;)我假设它的存在是为了与仍使用boost::asio::io_service
(也已弃用)的代码保持接口兼容性。
正如评论所反映的那样,我后来发现
io_context::strand
实际上并没有被弃用,尽管我看不出为什么会这样,仔细阅读该实现后,我得出的结论是
asio::strand<Executor>
严格说来更好混合两种服务不是最好的主意。事实上,这两项服务都使用相同的标签行进行了记录:
// Default service implementation for a strand.
我不禁觉得应该只有一个默认设置:)
现代线条不引用执行上下文,而是包装执行器。
虽然在技术上不同,但在概念上是相同的。
发布任务的用法相同:
post(s, task); // where s is either legacy or modern
defer(s, task);
dispatch(s, task);
事实上,您可能有与关联的执行者的任务,请参阅:
- When must you pass io_context to boost::asio::spawn? (C++)
- Which io_context does std::boost::asio::post / dispatch use?
您不能再使用遗留字符串构造IO服务对象(如tcp::Socket或STATE_TIMER)。这是因为不能将遗留链类型擦除到any_io_executor
:
using tcp = boost::asio::ip::tcp;
boost::asio::io_context ioc;
auto modern = make_strand(ioc.get_executor());
tcp::socket sock(modern);
boost::asio::io_context::strand legacy(ioc);
tcp::socket sock(legacy); // COMPILE ERROR
如果您确实想要,可以不使用any_io_executor
:
boost::asio::basic_stream_socket<tcp, decltype(legacy)> sock(legacy);
sock.connect({{}, 8989});
这篇关于Asio::strand<;asio::io_context::executor_type>;VS io_CONTEXT::Strand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Asio::strand<;asio::io_context::executor_type>;VS io_CONTEXT::Strand
基础教程推荐
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01