Fix examples to dispatch to strand

fix #1852
This commit is contained in:
Richard Hodges
2020-03-17 16:04:21 +01:00
parent 929b15f844
commit a21c1b7298
3 changed files with 36 additions and 2 deletions
@@ -21,6 +21,7 @@
#include <boost/beast/websocket.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/strand.hpp>
@@ -817,6 +818,20 @@ public:
// Launch the detector
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(
&detect_session::on_run,
this->shared_from_this()));
}
void
on_run()
{
// Set the timeout.
stream_.expires_after(std::chrono::seconds(30));