2017-04-05 16:27:13 +08:00
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <ctime>
|
2017-04-07 16:54:18 +08:00
|
|
|
#include <thread>
|
2017-04-05 16:27:13 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2017-04-07 16:54:18 +08:00
|
|
|
* mutex class TMutex which is used to synchronize threads. Default template
|
2017-04-05 16:27:13 +08:00
|
|
|
* value is std::mutex.
|
|
|
|
*/
|
|
|
|
template <typename TMutex = std::mutex>
|
|
|
|
class Scheduler {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param t - Time between executing function. If function is still running
|
2017-04-07 16:54:18 +08:00
|
|
|
* 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
|
2017-04-05 16:27:13 +08:00
|
|
|
* -1, scheduler will not run.
|
|
|
|
* @param f - Function which will be executed.
|
|
|
|
*/
|
|
|
|
Scheduler(const std::chrono::seconds &t, const std::function<void()> &f)
|
|
|
|
: pause_(t), func_(f) {
|
2017-04-07 16:54:18 +08:00
|
|
|
thread_ = std::thread([this]() {
|
|
|
|
// if pause_ equals -1, scheduler will not run
|
|
|
|
if (pause_ == std::chrono::seconds(-1)) return;
|
|
|
|
auto start_time = std::chrono::system_clock::now();
|
|
|
|
for (;;) {
|
|
|
|
if (!is_working_.load()) break;
|
2017-04-05 16:27:13 +08:00
|
|
|
|
2017-04-07 16:54:18 +08:00
|
|
|
func_();
|
2017-04-05 16:27:13 +08:00
|
|
|
|
2017-04-07 16:54:18 +08:00
|
|
|
std::unique_lock<std::mutex> lk(mutex_);
|
2017-04-05 16:27:13 +08:00
|
|
|
|
2017-04-07 16:54:18 +08:00
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
while (now >= start_time) start_time += pause_;
|
2017-04-05 16:27:13 +08:00
|
|
|
|
2017-04-07 16:54:18 +08:00
|
|
|
condition_variable_.wait_for(
|
|
|
|
lk, start_time - now, [&] { return is_working_.load() == false; });
|
|
|
|
lk.unlock();
|
|
|
|
}
|
2017-04-05 16:27:13 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-07 16:54:18 +08:00
|
|
|
~Scheduler() {
|
|
|
|
is_working_.store(false);
|
2017-04-05 16:27:13 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(mutex_);
|
|
|
|
condition_variable_.notify_one();
|
|
|
|
}
|
|
|
|
if (thread_.joinable()) thread_.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Variable is true when thread is running.
|
|
|
|
*/
|
|
|
|
std::atomic<bool> is_working_{true};
|
|
|
|
/**
|
|
|
|
* Time interval between function execution.
|
|
|
|
*/
|
|
|
|
const std::chrono::seconds pause_;
|
|
|
|
/**
|
|
|
|
* Function which scheduler executes.
|
|
|
|
*/
|
2017-04-07 16:54:18 +08:00
|
|
|
const std::function<void()> func_;
|
2017-04-05 16:27:13 +08:00
|
|
|
/**
|
|
|
|
* Mutex used to synchronize threads using condition variable.
|
|
|
|
*/
|
|
|
|
TMutex mutex_;
|
|
|
|
|
|
|
|
/**
|
2017-04-07 16:54:18 +08:00
|
|
|
* Condition variable is used to stop waiting until the end of the
|
|
|
|
* time interval if destructor is called.
|
2017-04-05 16:27:13 +08:00
|
|
|
*/
|
|
|
|
std::condition_variable condition_variable_;
|
|
|
|
/**
|
|
|
|
* Thread which runs function.
|
|
|
|
*/
|
|
|
|
std::thread thread_;
|
|
|
|
};
|