https://github.com/wolfulus/cpptimer
simple c++11 timer
https://github.com/wolfulus/cpptimer
cpp cpp11 header-only interval timeout timer timers
Last synced: 9 months ago
JSON representation
simple c++11 timer
- Host: GitHub
- URL: https://github.com/wolfulus/cpptimer
- Owner: WoLfulus
- License: mit
- Created: 2015-06-18T21:24:07.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-23T03:23:35.000Z (over 10 years ago)
- Last Synced: 2025-05-08T01:10:00.168Z (9 months ago)
- Topics: cpp, cpp11, header-only, interval, timeout, timer, timers
- Language: C++
- Size: 137 KB
- Stars: 6
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
cpptimer
============
Simple C++11 timers (interval, timeout, repeat).
In-process timers for periodic jobs that uses builder pattern for configuration. cpptimer lets you run any thing convertible to a **std::function** periodically at pre-determined intervals using a simple human-friendly syntax.
Features
--------
- Simple fluent/builder API
- Allows single or multi thread updates
- Thread-safe
- Single-file header only
- No dependencies
Requirements
------------
**cpptimer** requires a C++11 compiler only with `` and `` libraries.
Usage
-----
Creating the timer manager.
```c++
timer::manager mgr;
```
Registering tasks
```cpp
mgr.interval([]() {
cout << "Happens every 5 second" << endl;
}, std::chrono::seconds(5));
mgr.timeout([]() {
cout << "Happens once after 10 seconds" << endl;
}, std::chrono::seconds(10));
mgr.repeat([]() {
cout << "Happens 5 times every 3 seconds" << endl;
}, std::chrono::seconds(3), 5);
```
Canceling a task
```cpp
auto id = mgr.timeout(func, std::chrono::seconds(60));
...
mgr.cancel(id);
```
Updating the timers on your own thread
```cpp
while (true)
{
mgr.update();
boost::this_thread::sleep_for(
boost::chrono::milliseconds(200));
}
```
Leaving updates to the timer
*Note that this will create an additional thread and all updates will run under manager's thread*
```cpp
// default interval (250ms)
mgr.start();
// or with a custom interval for each update
mgr.start(std::chrono::milliseconds(200));
```
Distributed under the MIT license. See ``LICENSE`` for more information.