Fix gradiceks mess.

Summary: Fix fix fix.

Reviewers: matej.gradicek

Reviewed By: matej.gradicek

Differential Revision: https://phabricator.memgraph.io/D245
This commit is contained in:
Dominik Gleich 2017-04-07 10:54:18 +02:00
parent 22dfe61fe1
commit 74740dd162

View File

@ -1,13 +1,13 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <condition_variable> #include <condition_variable>
#include <thread>
#include <ctime> #include <ctime>
#include <thread>
/** /**
* Class used to run given std::function<void()>. Function is ran and then sleep * Class used to run given std::function<void()>. Function is ran and then sleep
* is called with parameter given in class constructor. Class is templated with * is called with parameter given in class constructor. Class is templated with
* mutex class TMutex which is used to synchronize threads. Default template * mutex class TMutex which is used to synchronize threads. Default template
* value is std::mutex. * value is std::mutex.
*/ */
template <typename TMutex = std::mutex> template <typename TMutex = std::mutex>
@ -15,37 +15,36 @@ class Scheduler {
public: public:
/** /**
* @param t - Time between executing function. If function is still running * @param t - Time between executing function. If function is still running
* when it should be ran again, it will not be ran and next * when it should be ran again, it will not be ran and next
* start time will be increased current time plus t. If t equals * start time will be increased current time plus t. If t equals
* -1, scheduler will not run. * -1, scheduler will not run.
* @param f - Function which will be executed. * @param f - Function which will be executed.
*/ */
Scheduler(const std::chrono::seconds &t, const std::function<void()> &f) Scheduler(const std::chrono::seconds &t, const std::function<void()> &f)
: pause_(t), func_(f) { : pause_(t), func_(f) {
thread_ = std::thread([this]() { thread_ = std::thread([this]() {
// if pause_ equals -1, scheduler will not run // if pause_ equals -1, scheduler will not run
if (pause_ == std::chrono::seconds(-1)) return; if (pause_ == std::chrono::seconds(-1)) return;
auto start_time = std::chrono::system_clock::now(); auto start_time = std::chrono::system_clock::now();
for(;;) { for (;;) {
if (!is_working_.load()) break; if (!is_working_.load()) break;
func_(); func_();
std::unique_lock<std::mutex> lk(mutex_); std::unique_lock<std::mutex> lk(mutex_);
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
while(now >= start_time) start_time += pause_; while (now >= start_time) start_time += pause_;
condition_variable_.wait_for(lk, start_time - now, [&] { condition_variable_.wait_for(
return is_working_.load() == false; lk, start_time - now, [&] { return is_working_.load() == false; });
}); lk.unlock();
lk.unlock(); }
}
}); });
} }
~Scheduler() { ~Scheduler() {
is_working_.store(false); is_working_.store(false);
{ {
std::unique_lock<std::mutex> lk(mutex_); std::unique_lock<std::mutex> lk(mutex_);
condition_variable_.notify_one(); condition_variable_.notify_one();
@ -65,15 +64,15 @@ class Scheduler {
/** /**
* Function which scheduler executes. * Function which scheduler executes.
*/ */
const std::function <void()> &func_; const std::function<void()> func_;
/** /**
* Mutex used to synchronize threads using condition variable. * Mutex used to synchronize threads using condition variable.
*/ */
TMutex mutex_; TMutex mutex_;
/** /**
* Condition variable is used to stop waiting until the end of the * Condition variable is used to stop waiting until the end of the
* time interval if destructor is called. * time interval if destructor is called.
*/ */
std::condition_variable condition_variable_; std::condition_variable condition_variable_;
/** /**