c++ - boost::asio::strand 和 boost::asio::io_context::strand 之间的区别
问题描述
我想使用 strand 序列化 http post 请求,以避免重叠写入网络。
我的方法是使用发送数据的回调post
从strand
对象调用方法,如以下代码所示:
void Send(
boost::beast::http::request<boost::beast::http::string_body> &req) {
strand_.post([=]() {
...
boost::beast::http::write(stream_, req);
...
}
但是,通过查看一些示例,我注意到两种类型的链定义:
boost::asio::strand<boost::asio::io_context::executor_type> strand_;
boost::asio::io_context::strand strand_;
知道每种类型之间有什么不同吗?
解决方案
他们之间其实并没有太大的区别。事实上,它们共享大量代码,这表明一个是从另一个创建的。
请参阅此处的差异(bosst 1.67)。
但是,boost::asio::strand
es 是一个最初打算用于的类模板asio::io_service
:
template <typename Executor>;
class strand
{
public:
/// The type of the underlying executor.
typedef Executor inner_executor_type;
/// Default constructor.
/**
* This constructor is only valid if the underlying executor type is default
* constructible.
*/
strand()
: executor_(),
impl_(use_service<detail::strand_executor_service>(
executor_.context()).create_implementation())
{
}
该类boost::asio::io_context::strand
不是类模板并且绑定到类boost::asio::io_context
:
class io_context::strand
{
public:
/// Constructor.
/**
* Constructs the strand.
*
* @param io_context The io_context object that the strand will use to
* dispatch handlers that are ready to be run.
*/
explicit strand(boost::asio::io_context& io_context)
: service_(boost::asio::use_service<
boost::asio::detail::strand_service>(io_context))
{
service_.construct(impl_);
}
推荐阅读
- ios - 在 iOS 14.2 上使用 VoIP 打开 CallKit 无法在终止状态下工作
- sql - TSQL:查询以最大大小的数据包分组文件
- powershell - Powershell 比较两个目录中的文件夹和子文件夹名称
- javascript - 类型错误:jQuery.getJSON 不是函数
- java - 使用 next() 和 nextInt() 方法
- c# - .NET Core 基于路由的身份验证与多个 B2C 环境
- bash - 如何在 BASH 中每隔一小时执行一次循环?
- regex - 复制 findstr 找到的文件?
- dictionary - 在 Ansible 中遍历包含子元素的字典
- ios - “类型'Bundle'没有成员'模块'”仅在fastlane中