-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_basic.cpp
More file actions
26 lines (20 loc) · 861 Bytes
/
Copy pathtimer_basic.cpp
File metadata and controls
26 lines (20 loc) · 861 Bytes
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
#include <simple_timer/simple_timer.h>
#include <iostream>
int64_t get_ms()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
int main()
{
std::cout << "[main start]: " << "main-thread-id = " << std::this_thread::get_id() << '\n';
SimpleTimer timer(std::chrono::seconds(1)); // 创建一个定时器, 每隔1秒执行一次
// 启动定时器
timer.start([]() {
std::cout << "timer task thread id = " << std::this_thread::get_id() << ", current ms:" << get_ms() % 100000 << '\n';
});
std::cout << "===== timer.start() =====\n";
std::this_thread::sleep_for(std::chrono::seconds(10));
timer.stop(); // 停止定时器
std::cout << "===== timer.stop() =====\n";
std::cout << "[main end]: " << "main-thread-id = " << std::this_thread::get_id() << '\n';
}