From 422c7ff8554861601d3f825030f3e3afcd71e507 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 28 Jan 2020 10:04:32 -0800 Subject: [PATCH] wait with a condition variable instead of a this_thread::sleep_for so that waiting can be cancelled when we stop/shutdown the data thread (see #151) --- ixwebsocket/IXWebSocket.cpp | 5 +++-- ixwebsocket/IXWebSocket.h | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index f794249f..cd9b0d78 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -169,6 +169,7 @@ namespace ix // wait until working thread will exit // it will exit after close operation is finished _stop = true; + _sleepCondition.notify_one(); _thread.join(); _stop = false; } @@ -282,8 +283,8 @@ namespace ix // Only sleep if we are retrying if (duration.count() > 0) { - // to do: make sleeping conditional - std::this_thread::sleep_for(duration); + std::unique_lock lock(_sleepMutex); + _sleepCondition.wait_for(lock, duration); } // Try to connect synchronously diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index 4591f0e3..015171b0 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -22,6 +22,7 @@ #include #include #include +#include namespace ix { @@ -140,6 +141,10 @@ namespace ix static const uint32_t kDefaultMaxWaitBetweenReconnectionRetries; uint32_t _maxWaitBetweenReconnectionRetries; + // Make the sleeping in the automatic reconnection cancellable + std::mutex _sleepMutex; + std::condition_variable _sleepCondition; + std::atomic _handshakeTimeoutSecs; static const int kDefaultHandShakeTimeoutSecs;