Skip to content

Commit

Permalink
Add timer class with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Armstrong committed Sep 14, 2024
1 parent a60996e commit 75cfd93
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tesseract_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ add_library(
src/plugin_info.cpp
src/resource_locator.cpp
src/types.cpp
src/stopwatch.cpp)
src/stopwatch.cpp
src/timer.cpp)
target_link_libraries(
${PROJECT_NAME}
PUBLIC Eigen3::Eigen
Expand Down
67 changes: 67 additions & 0 deletions tesseract_common/include/tesseract_common/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @file timer.h
* @brief Simple timer class using chrono and thread
*
* @author Levi Armstrong
* @date February 2, 2021
* @version TODO
* @bug No known bugs
*
* @copyright Copyright (c) 2021, Southwest Research Institute
*
* @par License
* Software License Agreement (Apache License)
* @par
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* @par
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TESSERACT_COMMON_TIMER_H
#define TESSERACT_COMMON_TIMER_H

#include <tesseract_common/macros.h>
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#include <iostream>
#include <functional>
#include <chrono>
#include <thread>
#include <atomic>
TESSERACT_COMMON_IGNORE_WARNINGS_POP

namespace tesseract_common
{
/** @brief A timer which calls a callback every interval on a separate thread */
class Timer
{
public:
Timer() = default;
~Timer();
Timer(const Timer&) = delete;
Timer& operator=(const Timer&) = delete;
Timer(Timer&&) = delete;
Timer& operator=(Timer&&) = delete;

/**
* @brief Start the timer with a callback function and a std::chrono::duration interval
* @param callback The callback called every time the timer expires
* @param interval The interval at which the timer triggers
*/
void start(const std::function<void()>& callback, std::chrono::steady_clock::duration interval);

/** @brief Stop the timer */
void stop();

private:
std::atomic<bool> running_{ false };
std::thread timer_thread_;
};

} // namespace tesseract_common
#endif // TESSERACT_COMMON_TIMER_H
53 changes: 53 additions & 0 deletions tesseract_common/src/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @file timer.cpp
* @brief Simple timer class using chrono and thread
*
* @author Levi Armstrong
* @date February 2, 2021
* @version TODO
* @bug No known bugs
*
* @copyright Copyright (c) 2021, Southwest Research Institute
*
* @par License
* Software License Agreement (Apache License)
* @par
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* @par
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <tesseract_common/timer.h>

namespace tesseract_common
{
Timer::~Timer() { stop(); }

void Timer::start(const std::function<void()>& callback, std::chrono::steady_clock::duration interval)
{
running_ = true;
timer_thread_ = std::thread([this, callback, interval]() {
while (running_)
{
auto next_time = std::chrono::steady_clock::now() + interval;
callback(); // Call the provided function
std::this_thread::sleep_until(next_time);
}
});
}

void Timer::stop()
{
running_ = false;
if (timer_thread_.joinable())
timer_thread_.join();
}

} // namespace tesseract_common

0 comments on commit 75cfd93

Please sign in to comment.