forked from Bosma/Scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
45 lines (34 loc) · 1.67 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include "Scheduler.h"
void message(const std::string &s) {
std::cout << s << std::endl;
}
int main() {
// number of tasks that can run simultaneously
// Note: not the number of tasks that can be added,
// but number of tasks that can be run in parallel
unsigned int max_n_threads = 12;
// Make a new scheduling object.
// Note: s cannot be moved or copied
Bosma::Scheduler s(max_n_threads);
// every second call message("every second")
s.every(std::chrono::seconds(1), message, "every second");
// in one minute
s.in(std::chrono::minutes(1), []() { std::cout << "in one minute" << std::endl; });
// run lambda, then wait a second, run lambda, and so on
// different from every in that multiple instances of the function will never be run concurrently
s.interval(std::chrono::seconds(1), []() {
std::cout << "right away, then once every 6s" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
});
// https://en.wikipedia.org/wiki/Cron
s.cron("* * * * *", []() { std::cout << "top of every minute" << std::endl; });
// Time formats supported:
// %Y/%m/%d %H:%M:%S, %Y-%m-%d %H:%M:%S, %H:%M:%S
// With only a time given, it will run tomorrow if that time has already passed.
// But with a date given, it will run immediately if that time has already passed.
s.at("2017-04-19 12:31:15", []() { std::cout << "at a specific time." << std::endl; });
s.cron("5 0 * * *", []() { std::cout << "every day 5 minutes after midnight" << std::endl; });
// destructor of Bosma::Scheduler will cancel all schedules but finish any tasks currently running
std::this_thread::sleep_for(std::chrono::minutes(10));
}