c++ - 如何在 Visual C++ 2012 中 sleep 不受系统时钟调整影响的固定时间

标签 c++ visual-c++ c++11 time thread-sleep

我正在使用 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/

相关文章:

c++ - 当 x 为 const int[] 时,x[0] == 1 C++11 中的常量表达式?

c++ - 使用 Eigen 查找两个列表之间的差异

c++ - VC++ 与 G++,cout

c++ - gluTessCallback 指向回调函数的指针。编译错误

c++ - 错误 C1083 无法打开包含文件 : 'IexBaseExc.h' : No such file or directory

windows - C++11:如何实现快速、轻量级、公平的同步资源访问

c++ - 没有对象的模板打印成员变量

c++ - 标准的意图是任何形式的 `nested-type-specifier followed by an identifier` 都可以称为合格 ID

c++ - static_cast 和 reinterpret_cast 效果一样的时候用哪个?

c++ - lambda 函数/表达式是否支持 constexpr?