我正在使用 std::this_thread::sleep_for(std::chrono::seconds(1));
休眠一秒钟。我发现,如果我在休眠期间将系统时间向后调整,休眠时间会延长我刚刚调整的时间。
但 std::this_thread::sleep_for()
应该不管系统时间如何工作,这与 std::this_thread::sleep_until()
不同,后者可能应该展示上述行为。
当我查看 Visual C++ 2012 的 std::this_thread::sleep_for()
实现时,我发现了
template<class _Rep,
class _Period> inline
void sleep_for(const chrono::duration<_Rep, _Period>& _Rel_time)
{ // sleep for duration
stdext::threads::xtime _Tgt = _To_xtime(_Rel_time);
sleep_until(&_Tgt);
}
所以sleep_for()
是在Visual C++ 2012中使用sleep_until()
实现的。我查了下C++11标准,其实并没有禁止这种的实现。那么如何才能在不受系统时钟调整影响的固定时间段内休眠呢?
最佳答案
你可能应该使用 timer queues这些是精确的相对计时器。要使计时器可等待,请让您的计时器例程触发一个事件,如 this example .
关于c++ - 如何在 Visual C++ 2012 中 sleep 不受系统时钟调整影响的固定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19727189/