我正在从这个例子中学习 Boost.Beast 和 Boost.Asiolibs/beast/example/http/server/async-ssl/http_server_async_ssl.cpp - 1.77.0.
据我所知,I/O 对象上发生的所有 I/O 操作都发生在该对象的 I/O 执行上下文中。异步操作将在同一个线程中run
I/O 上下文,因为它们都被(间接)调用run
I/O 上下文。
在此示例中(请参阅上面的链接),当建立连接时,接受器为新连接分配专用链:
do_accept()
{
// The new connection gets its own strand
acceptor_.async_accept(
net::make_strand(ioc_),
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
这是否意味着新连接上发生的所有 I/O 操作都发生在链中?如果是这样,为什么示例使用net::dispatch
在调用时再次指定链async_read
?
// Start the asynchronous operation
void
run()
{
// We need to be executing within a strand to perform async operations
// on the I/O objects in this session. Although not strictly necessary
// for single-threaded contexts, this example code is written to be
// thread-safe by default.
net::dispatch(
stream_.get_executor(),
beast::bind_front_handler(
&session::on_run,
shared_from_this()));
}
如果我们只调用有什么区别async_read
直接不经过net::dispatch
?谢谢。 :)