【发布时间】:2017-08-29 14:19:53
【问题描述】:
我想执行一个循环并在假设 2 分钟后退出这个循环。
while(condition) {
// do stuff
// exit this loop after 2 minutes
}
有人可以推荐我最好的方法吗?
根据答案,这是我所做的:
time_t futur = time(NULL) + 120;
while(condition) {
// do stuff
if(time(NULL) > futur) {
break;
}
}
【问题讨论】:
-
使用 std::chrono
-
最便携的解决方案是使用
std::chrono提供的计时功能。 -
如果两分钟后,body 还在执行,会发生什么?是否应该终止执行?