From 383b9cb7f2eb2fa01f1887e8c96f98cedeb3794d Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 21 Jan 2019 16:07:00 -0800 Subject: [PATCH] Send from the strand --- CHANGELOG.md | 6 ++++++ .../websocket/server/chat-multi/websocket_session.cpp | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec423a4d..39a3355c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +Version 207 + +* Send from the strand + +-------------------------------------------------------------------------------- + Version 206 * Clear error codes idiomatically diff --git a/example/websocket/server/chat-multi/websocket_session.cpp b/example/websocket/server/chat-multi/websocket_session.cpp index 7540544d..66fdf835 100644 --- a/example/websocket/server/chat-multi/websocket_session.cpp +++ b/example/websocket/server/chat-multi/websocket_session.cpp @@ -90,6 +90,17 @@ void websocket_session:: send(boost::shared_ptr const& ss) { + // Get on the strand if we aren't already, + // otherwise we will concurrently access + // objects which are not thread-safe. + if(! strand_.running_in_this_thread()) + return net::post( + net::bind_executor(strand_, + std::bind( + &websocket_session::send, + shared_from_this(), + ss))); + // Always add to queue queue_.push_back(ss);