Fix data race in http server examples

When using `beast::tcp_stream`, the user must make sure that async operations
are initiated from within the strand associated with the stream.

Signed-off-by: Damian Jarek <damian.jarek93@gmail.com>
This commit is contained in:
Damian Jarek
2019-10-06 07:52:13 -07:00
committed by Vinnie Falco
parent 9170a0daaa
commit 3817fb4c94
5 changed files with 73 additions and 15 deletions
@@ -20,6 +20,7 @@
#include <boost/beast/ssl.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/strand.hpp>
#include <boost/config.hpp>
#include <algorithm>
@@ -304,7 +305,16 @@ public:
void
run()
{
loop({}, 0, false);
// 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::loop,
shared_from_this(),
beast::error_code{},
0,
false));
}
#include <boost/asio/yield.hpp>