Go to the documentation of this file.00001 #ifndef PERIODIC_TIMER_HPP
00002 #define PERIODIC_TIMER_HPP
00003
00004 #include <atomic>
00005 #include <chrono>
00006 #include <functional>
00007 #include <memory>
00008 #include <mutex>
00009 #include <thread>
00010
00011 namespace msp {
00012
00013 class PeriodicTimer {
00014 public:
00020 PeriodicTimer(const std::function<void()> funct,
00021 const double period_seconds);
00022
00026 ~PeriodicTimer() { stop(); }
00027
00031 bool start();
00032
00036 bool stop();
00037
00042 double getPeriod() const { return period_us.count() / 1.e6; }
00043
00049 void setPeriod(const double& period_seconds);
00050
00051 private:
00052 std::shared_ptr<std::thread> thread_ptr;
00053 std::function<void()> funct;
00054 std::chrono::duration<size_t, std::micro> period_us;
00055 std::timed_mutex mutex_timer;
00056 std::chrono::steady_clock::time_point tstart;
00057
00058 std::atomic_flag running_ = ATOMIC_FLAG_INIT;
00059 };
00060
00061 }
00062
00063 #endif