【发布时间】:2021-10-17 03:43:19
【问题描述】:
我正在从这个例子中学习 Boost.Beast 和 Boost.Asio libs/beast/example/http/server/async-ssl/http_server_async_ssl.cpp - 1.77.0。
据我所知,发生在 I/O 对象上的所有 I/O 操作都发生在对象的 I/O 执行上下文中。异步操作将与 I/O 上下文的 run 在同一线程中,因为它们都由 I/O 上下文的 run 调用(间接)。
在这个例子中(请看上面的链接),当连接建立时,接受器分配一个专用链给新连接:
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 操作都发生在链中?如果是这样,为什么示例在调用async_read 时使用net::dispatch 再次指定链?
// 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有什么区别?谢谢。 :)
【问题讨论】:
标签: c++ boost-asio boost-beast