C++ 时间处理与错误处理:从基础到实践
1. C++ 中的延迟函数
在 C++ 编程里,延迟函数能让程序在特定时间点或时间段内暂停执行。这里主要介绍 sleep_for 和 sleep_until 两个函数。
以下是 sleep_until 函数的代码:
void sleep_until(int count,
std::chrono::milliseconds delay) {
auto wake_up = std::chrono::system_clock::now();
for (int i = 0; i < 10; i++) {
wake_up += delay;
auto start = std::chrono::system_clock::now();
std::this_thread::sleep_until(wake_up);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> delta = end - start;
std::cout << "Sleep until: " << delta.count() << std::endl;
}
}
主函数调用示例:
C++ 时间与错误处理全解析
超级会员免费看
订阅专栏 解锁全文
22万+

被折叠的 条评论
为什么被折叠?



