ASIO :: async_write和钢绞线 [英] asio::async_write and strand
问题描述
asio::async_write(m_socket, asio::buffer(buf, bytes),
custom_alloc(m_strand.wrap(custom_alloc(_OnSend))));
这是否code保证所有异步操作处理器(调用async_write_some)内async_write通过链叫什么名字? (或者它只是为的 my_handler 的?)
推荐答案
通过以下code:
With the following code:
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
的返回类型可以是:
-
从<返回的确切类型href=\"http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/reference/io_service__strand/wrap.html\"相对=nofollow>
链::包()
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};
}
请参阅这回答链上的更多详细信息,以及的这个回答的详细信息, asio_handler_invoke
。
See this answer for more details on strands, and this answer for details on asio_handler_invoke
.
这篇关于ASIO :: async_write和钢绞线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!