asio::async_write and strand(asio::async_write 和链)
问题描述
asio::async_write(m_socket, asio::buffer(buf, bytes),
custom_alloc(m_strand.wrap(custom_alloc(_OnSend))));
这段代码是否保证async_write内部的所有异步操作处理程序(调用async_write_some)都通过strand调用?(或者它只是为了 my_handler?)
Does this code guarantee that all asynchronous operation handlers(calls to async_write_some) inside async_write are called through strand? (or it's just for my_handler?)
推荐答案
使用以下代码:
asio::async_write(stream, ..., custom_alloc(m_strand.wrap(...)));
对于这个组合操作,如果以下所有条件都为真,所有对 stream.async_write_some()
的调用都将在 m_strand
内调用:
For this composed operation, all calls to stream.async_write_some()
will be invoked within m_strand
if all of the following conditions are true:
发起的
async_write(...)
调用在m_strand()
中运行:
assert(m_strand.running_in_this_thread());
asio::async_write(stream, ..., custom_alloc(m_strand.wrap(...)));
custom_alloc
的返回类型是:
从
strand::wrap()
template <typename Handler>
Handler custom_alloc(Handler) { ... }
一个自定义处理程序,用于适当链接 asio_handler_invoke()
:
template <class Handler>
class custom_handler
{
public:
custom_handler(Handler handler)
: handler_(handler)
{}
template <class... Args>
void operator()(Args&&... args)
{
handler_(std::forward<Args>(args)...);
}
template <typename Function>
friend void asio_handler_invoke(
Function intermediate_handler,
custom_handler* my_handler)
{
// Support chaining custom strategies incase the wrapped handler
// has a custom strategy of its own.
using boost::asio::asio_handler_invoke;
asio_handler_invoke(intermediate_handler, &my_handler->handler_);
}
private:
Handler handler_;
};
template <typename Handler>
custom_handler<Handler> custom_alloc(Handler handler)
{
return {handler};
}
请参阅此答案以了解有关链的更多详细信息,以及this 回答有关 asio_handler_invoke
的详细信息.
See this answer for more details on strands, and this answer for details on asio_handler_invoke
.
这篇关于asio::async_write 和链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:asio::async_write 和链


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01