问题描述
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 和链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)