Update periodic_worker.h

This commit is contained in:
Gabi Melman 2018-07-24 03:08:49 +03:00 committed by GitHub
parent 40aeaaee54
commit 516a8e4212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,6 @@
// creates the thread on construction. // creates the thread on construction.
// stops and joins the thread on destruction. // stops and joins the thread on destruction.
#include <atomic>
#include <chrono> #include <chrono>
#include <condition_variable> #include <condition_variable>
#include <functional> #include <functional>
@ -55,16 +54,20 @@ public:
// stop the back thread and join it // stop the back thread and join it
~periodic_worker() ~periodic_worker()
{ {
if (active_) if (!active_)
{ {
return;
}
{
std::lock_guard<std::mutex> lock(mutex_);
active_ = false; active_ = false;
}
cv_.notify_one(); cv_.notify_one();
flusher_thread_.join(); flusher_thread_.join();
} }
}
private: private:
std::atomic<bool> active_; bool active_;
std::thread flusher_thread_; std::thread flusher_thread_;
std::mutex mutex_; std::mutex mutex_;
std::condition_variable cv_; std::condition_variable cv_;