Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ortfero/timetable
C++17 one-header library to invoke something periodically or at the specified time
https://github.com/ortfero/timetable
Last synced: 3 months ago
JSON representation
C++17 one-header library to invoke something periodically or at the specified time
- Host: GitHub
- URL: https://github.com/ortfero/timetable
- Owner: ortfero
- License: mit
- Created: 2021-07-04T23:11:00.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-28T09:18:13.000Z (5 months ago)
- Last Synced: 2024-08-28T10:39:00.252Z (5 months ago)
- Language: C++
- Homepage:
- Size: 111 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesomecpp - timetable - - C++17 one-header library to invoke something periodically or at the specified time. (Scheduling)
README
# timetable
C++17 one-header library to invoke something periodically or at specified time## Synopsis
```cpp
namespace timetable {struct context {
virtual ~context() { }
};
template
class scheduler {
public:
using handler = std::function;scheduler(duration const& granularity = std::chrono::milliseconds{500});
scheduler(scheduler const&) = delete;
scheduler& operator = (scheduler const&) = delete;
~scheduler();template
task_id schedule_from_time(time_point time_at,
duration const& interval,
F&& handler,
std::unique_ptr context = nullptr);
template
task_id schedule_from_now(duration const& interval,
F&& handler,
std::unique_ptr context = nullptr);
bool unschedule(task_id tid) noexcept;
template
task_id schedule_daily_at(duration const& time_of_day,
F&& handler,
std::unique_ptr context = nullptr);
template
task_id schedule_every_hour(F&& handler,
std::unique_ptr context = nullptr);
template
task_id schedule_every_minute(F&& handler,
std::unique_ptr context = nullptr);
template
task_id schedule_every_second(F&& handler,
std::unique_ptr context = nullptr);
template
task_id schedule_once(time_point time_at,
F&& handler,
std::unique_ptr context = nullptr);
void pass(); // pass scheduling
void run(); // run in a separate thread
void stop();
}; // scheduler
} // namespace timetable
```## Snippets
### Set scheduler precision
```cpp
#include
#include
...
auto scheduler = timetable::scheduler{std::chrono::milliseconds{300}};
```### Run task periodically from now
```cpp
#include
#include
#include
#includeint main() {
auto scheduler = timetable::scheduler{};
scheduler.schedule_from_now(std::chrono::seconds{1},
[](std::chrono::system_clock::time_point){ std::puts("one second"); });
scheduler.run();
std::this_thread::sleep_for(std::chrono::seconds{10});
scheduler.stop();
return 0;
}
```### Run task daily at specified time
```cpp
#include
#include
#include
#includeint main() {
auto scheduler = timetable::scheduler{};
using namespace std::chrono;
auto const at = hours{13} + minutes{13} + seconds{13};
scheduler.schedule_daily_at(at,
[](std::chrono::system_clock::time_point){ std::puts("now 13:13:13 utc"); });
scheduler.run();
std::this_thread::sleep_for(minutes{5});
scheduler.stop();
return 0;
}
```### Remove scheduled task
```cpp
#include
...
auto scheduler = timetable::scheduler{};auto const task_id = scheduler.schedule_from_now(std::chrono::hours{1},
[](std::chrono::system_clock::time_point){ std::puts("1 hour passed"); });
scheduler.unschedule(task_id);
```