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 1c7ccbae12
commit 422c7ff855
2 changed files with 8 additions and 2 deletions

View File

@ -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<std::mutex> lock(_sleepMutex);
_sleepCondition.wait_for(lock, duration);
}
// Try to connect synchronously

View File

@ -22,6 +22,7 @@
#include <mutex>
#include <string>
#include <thread>
#include <condition_variable>
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<int> _handshakeTimeoutSecs;
static const int kDefaultHandShakeTimeoutSecs;