I'm learning Boost.Beast & Boost.Asio from this example libs/beast/example/http/server/async-ssl/http_server_async_ssl.cpp - 1.77.0.
As far as I know, all I/O operations that happen on a I/O object happen in the I/O execution context of the object. The asynchronous operations will be in the same thread as the run
of the I/O context, because they are all called (indirectly) by run
of the I/O context.
In this example (see above link please), when the connection is established, the acceptor assigns a dedicated strand to the new connection:
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()));
}
Does it imply that all I/O operations that happen on the new connections happen in the strand? If so, why does the example use net::dispatch
to specify the strand AGAIN when it is going to call 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()));
}
What is the difference if we just call the async_read
directly without going through net::dispatch
? Thanks. :)