使用std::chrono和std::thread可实现高精度跨平台计时器,支持单次与周期性任务。1. 通过steady_clock测量时间间隔;2. 结合sleep_until实现延时执行;3. 封装Timer类管理线程安全的定时任务;4. 多定时任务宜用优先队列统一调度以提升效率。

在C++中实现一个计时器(Timer),尤其是结合多线程和高精度时间测量时,需要使用现代C++标准库中的 std::chrono 和 std::thread。这种方式不仅跨平台,而且能提供微秒甚至纳秒级的精度。
C++11 引入了 std::chrono,它提供了对时间点(time_point)、时间段(duration)和时钟(clock)的封装,适合做高精度计时。
常用时钟类型:
示例:测量代码执行时间
立即学习“C++免费学习笔记(深入)”;
#include <iostream>
#include <chrono>
<p>int main() {
auto start = std::chrono::steady_clock::now();</p><pre class='brush:php;toolbar:false;'>// 模拟耗时操作
for (int i = 0; i < 1000000; ++i) {
// do nothing
}
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "耗时: " << duration.count() << " 微秒\n";
return 0;}
可以利用 std::thread 和 std::this_thread::sleep_until 来实现一个简单的定时器。
目标:在指定时间后执行函数,或周期性执行。
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>
<p>void simple_timer(int delay_ms, std::function<void()> task) {
auto wake_time = std::chrono::steady_clock::now() +
std::chrono::milliseconds(delay_ms);
std::this_thread::sleep_until(wake_time);
task();
}</p><p>// 周期性执行
void periodic_timer(int interval_ms, int times, std::function<void()> task) {
for (int i = 0; i < times; ++i) {
auto next_time = std::chrono::steady_clock::now() +
std::chrono::milliseconds(interval_ms);
task();
std::this_thread::sleep_until(next_time);
}
}</p>调用示例:
int main() {
simple_timer(1000, []() {
std::cout << "1秒后执行\n";
});
<pre class='brush:php;toolbar:false;'>periodic_timer(500, 3, []() {
std::cout << "周期任务执行\n";
});
return 0;}
如果要支持多个定时任务并行运行,应将每个定时器放在独立线程中,避免阻塞主线程。
class Timer {
public:
Timer() : running(false) {}
<pre class='brush:php;toolbar:false;'>~Timer() {
stop();
}
void start_once(int delay_ms, std::function<void()> task) {
stop();
running = true;
thread = std::thread([=]() {
auto target = std::chrono::steady_clock::now() +
std::chrono::milliseconds(delay_ms);
std::this_thread::sleep_until(target);
if (running) task();
});
}
void start_periodic(int interval_ms, std::function<void()> task) {
stop();
running = true;
thread = std::thread([=]() {
while (running) {
auto next_time = std::chrono::steady_clock::now() +
std::chrono::milliseconds(interval_ms);
task();
std::this_thread::sleep_until(next_time);
}
});
}
void stop() {
running = false;
if (thread.joinable()) {
thread.join();
}
}private: std::thread thread; bool running; };
使用方式:
int main() {
Timer timer;
timer.start_once(1000, []() {
std::cout << "一次性任务\n";
});
<pre class='brush:php;toolbar:false;'>std::this_thread::sleep_for(std::chrono::seconds(2));
timer.start_periodic(300, []() {
std::cout << "每300ms执行一次\n";
});
std::this_thread::sleep_for(std::chrono::seconds(2));
return 0;}
基本上就这些。C++ 中实现 Timer 不复杂,关键是选对时钟和线程控制方式。
以上就是C++怎么实现一个计时器(Timer)_C++多线程与高精度时间测量的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号