timer.cpp
Go to the documentation of this file.
1 
27 #include <tesseract_common/timer.h>
28 
29 namespace tesseract_common
30 {
32 
33 void Timer::start(const std::function<void()>& callback, std::chrono::steady_clock::duration interval)
34 {
35  running_ = true;
36  timer_thread_ = std::thread([this, callback, interval]() {
37  while (running_)
38  {
39  auto next_time = std::chrono::steady_clock::now() + interval;
40  callback(); // Call the provided function
41  std::this_thread::sleep_until(next_time);
42  }
43  });
44 }
45 
47 {
48  running_ = false;
49  if (timer_thread_.joinable())
50  timer_thread_.join();
51 }
52 
53 } // namespace tesseract_common
tesseract_common::Timer::start
void start(const std::function< void()> &callback, std::chrono::steady_clock::duration interval)
Start the timer with a callback function and a std::chrono::duration interval.
Definition: timer.cpp:33
tesseract_common
Definition: allowed_collision_matrix.h:19
tesseract_common::Timer::timer_thread_
std::thread timer_thread_
Definition: timer.h:62
tesseract_common::Timer::~Timer
~Timer()
Definition: timer.cpp:31
timer.h
Simple timer class using chrono and thread.
tesseract_common::Timer::running_
std::atomic< bool > running_
Definition: timer.h:61
tesseract_common::Timer::stop
void stop()
Stop the timer.
Definition: timer.cpp:46


tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40