Protect the timer to avoid "cancel" and "refresh" happens at the same time.
Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
parent
c7f17cdf0d
commit
8395e821fc
|
|
@ -5,6 +5,7 @@
|
|||
#include <chrono>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
|
|
@ -117,6 +118,9 @@ namespace etcd
|
|||
|
||||
int ttl;
|
||||
int64_t lease_id;
|
||||
|
||||
// protect the initializing status of `timer`.
|
||||
std::mutex mutex_for_refresh_;
|
||||
std::atomic_bool continue_next;
|
||||
|
||||
// grpc timeout in `refresh()`
|
||||
|
|
|
|||
|
|
@ -124,6 +124,7 @@ etcd::KeepAlive::~KeepAlive()
|
|||
|
||||
void etcd::KeepAlive::Cancel()
|
||||
{
|
||||
std::lock_guard<std::mutex> scope_lock(mutex_for_refresh_);
|
||||
if (!continue_next.exchange(false)) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -142,6 +143,7 @@ void etcd::KeepAlive::Check() {
|
|||
|
||||
void etcd::KeepAlive::refresh()
|
||||
{
|
||||
std::lock_guard<std::mutex> scope_lock(mutex_for_refresh_);
|
||||
if (!continue_next.load()) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue