PeriodicTimer.hpp
Go to the documentation of this file.
1 #ifndef PERIODIC_TIMER_HPP
2 #define PERIODIC_TIMER_HPP
3 
4 #include <atomic>
5 #include <chrono>
6 #include <functional>
7 #include <memory>
8 #include <mutex>
9 #include <thread>
10 
11 namespace msp {
12 
14 public:
20  PeriodicTimer(const std::function<void()> funct,
21  const double period_seconds);
22 
27 
31  bool start();
32 
36  bool stop();
37 
42  double getPeriod() const { return period_us.count() / 1.e6; }
43 
49  void setPeriod(const double& period_seconds);
50 
51 private:
52  std::shared_ptr<std::thread> thread_ptr;
53  std::function<void()> funct;
54  std::chrono::duration<size_t, std::micro> period_us;
55  std::timed_mutex mutex_timer;
56  std::chrono::steady_clock::time_point tstart;
57 
58  std::atomic_flag running_ = ATOMIC_FLAG_INIT;
59 };
60 
61 } // namespace msp
62 
63 #endif
bool start()
start define and start background thread
std::atomic_flag running_
bool stop()
stop tell thread to stop and wait for end
~PeriodicTimer()
PeriodicTimer destructor.
std::function< void()> funct
void setPeriod(const double &period_seconds)
setPeriod change the update period of timer thread This will stop and restart the thread...
std::shared_ptr< std::thread > thread_ptr
std::chrono::duration< size_t, std::micro > period_us
std::timed_mutex mutex_timer
std::chrono::steady_clock::time_point tstart
double getPeriod() const
getPeriod get period in seconds
PeriodicTimer(const std::function< void()> funct, const double period_seconds)
PeriodicTimer define a periodic timer.


msp
Author(s): Christian Rauch
autogenerated on Tue Oct 6 2020 03:39:02