计数退出循环样例程序
#include <iostream>
#include <chrono>
#include <thread>
int main() {
// 设置循环的最大执行时间(例如,5秒)
auto start_time = std::chrono::steady_clock::now();
auto end_time = start_time + std::chrono::seconds(5);
while (std::chrono::steady_clock::now() < end_time) {
// 执行一些操作
std::cout << "循环中..." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 模拟工作
}
std::cout << "达到设定时间,退出循环。" << std::endl;
return 0;
}

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



