diff --git a/CHANGELOG.md b/CHANGELOG.md index 11b0968b..69c4599c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Version XXX: * Remove CODE_OF_CONDUCT.md * Refactor websocket read * Correct buffer_bytes documentation +* Fix examples to dispatch to strand -------------------------------------------------------------------------------- diff --git a/example/advanced/server-flex/advanced_server_flex.cpp b/example/advanced/server-flex/advanced_server_flex.cpp index 1d46c24c..b0c889bf 100644 --- a/example/advanced/server-flex/advanced_server_flex.cpp +++ b/example/advanced/server-flex/advanced_server_flex.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -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)); diff --git a/example/advanced/server/advanced_server.cpp b/example/advanced/server/advanced_server.cpp index 014dd293..715f8b8c 100644 --- a/example/advanced/server/advanced_server.cpp +++ b/example/advanced/server/advanced_server.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -439,9 +440,18 @@ public: void run() { - do_read(); + // 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( + &http_session::do_read, + this->shared_from_this())); } + private: void do_read() @@ -588,7 +598,15 @@ public: void run() { - do_accept(); + // 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( + acceptor_.get_executor(), + beast::bind_front_handler( + &listener::do_accept, + this->shared_from_this())); } private: