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)

This commit is contained in:
Benjamin Sergeant 2020-01-28 10:04:32 -08:00
parent 1ee8e31483
commit 78af792f23
2 changed files with 8 additions and 2 deletions

View File

@ -169,6 +169,7 @@ namespace ix
// wait until working thread will exit // wait until working thread will exit
// it will exit after close operation is finished // it will exit after close operation is finished
_stop = true; _stop = true;
_sleepCondition.notify_one();
_thread.join(); _thread.join();
_stop = false; _stop = false;
} }
@ -282,8 +283,8 @@ namespace ix
// Only sleep if we are retrying // Only sleep if we are retrying
if (duration.count() > 0) if (duration.count() > 0)
{ {
// to do: make sleeping conditional std::unique_lock<std::mutex> lock(_sleepMutex);
std::this_thread::sleep_for(duration); _sleepCondition.wait_for(lock, duration);
} }
// Try to connect synchronously // Try to connect synchronously

View File

@ -22,6 +22,7 @@
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
#include <condition_variable>
namespace ix namespace ix
{ {
@ -140,6 +141,10 @@ namespace ix
static const uint32_t kDefaultMaxWaitBetweenReconnectionRetries; static const uint32_t kDefaultMaxWaitBetweenReconnectionRetries;
uint32_t _maxWaitBetweenReconnectionRetries; uint32_t _maxWaitBetweenReconnectionRetries;
// Make the sleeping in the automatic reconnection cancellable
std::mutex _sleepMutex;
std::condition_variable _sleepCondition;
std::atomic<int> _handshakeTimeoutSecs; std::atomic<int> _handshakeTimeoutSecs;
static const int kDefaultHandShakeTimeoutSecs; static const int kDefaultHandShakeTimeoutSecs;