Simple library to repeatedly call a member function, free function or lambda at a given time interval.
- Use a variety of callback types:
- Class member functions via
std::bind
- Free functions
- Lambdas
- Class member functions via
- RAII cleanup, don't have to worry about explicitly calling
stop()
. - Reliable function timing (tested to be within ~1 millisecond)
- Auto-recovery if callback takes longer than interval time.
#include <periodic_function/periodic_function.hpp>
// call function every 300 milliseconds
dp::periodic_function heartbeat([]() {
// do something here...
}, 300U);
// start calling function
heartbeat.start();
// optional: stop calling the function
// function will stop being called when object goes out of scope
heartbeat.stop();
How callbacks that exceed the interval are handled is passed on a template argument policy class. The current policies available are:
This will schedule the callback to be called again on the next interval timeout (the interval that was missed is skipped). This is good to use if the callback is not expected to exceed the interval time.
This will schedule the callback to be called immediately and then control will be given back to the timer which will operate at the regular interval.
periodic-function
requires C++17 support and has been tested with:
- Visual Studio 2019 (msvc)
- Visual Studio 2019 (clang-cl)
- Ubuntu 18/20.04 GCC 10
- Ubuntu 18/20.04 Clang 10
Use the following commands to build the project
cmake -S <source_dir> -B build
cmake --build build --config Release
To run tests, simple cd
into the build directory and run:
ctest --build-config Debug
Contributions are very welcome. Please see contribution guidelines for more info.
The project is licensed under the MIT license. See LICENSE for more details.
@DeveloperPaul123 |
---|
🚧 MacOS support is still a WIP. 🚧