我们使用 boost:chrono 来实现定时器。下面是我们的实现:
boost::chrono::time_point<boost::chrono::steady_clock> untilTime(boost::chrono::steady_clock::now() + boost::chrono::milliseconds(dueTime));
boost::condition_variable m_condition;
(void)m_condition.wait_until(guard, untilTime);
在 Ubuntu Linux 64 位(使用 12.04 和 15.04 测试)上的计算机时间被设置为大于计时器间隔的时间后,计时器立即到期。
我们已经用不同的 boost 版本 1.51、1.54、1.55、1.57、1.59 重现了这个问题
在 Ubuntu Linux 32 位(版本 12.04、13.04)和 Windows 7(32 位和 64 位)上无法重现问题。
最佳答案
http://en.cppreference.com/w/cpp/chrono/steady_clock
不是规范。
N4527是规范,尽管不是官方规范。它是 C++ 规范的最新工作草案。它在 20.12.7.2/p1 中说:
Objects of class
steady_clock
represent clocks for which values oftime_point
never decrease as physical time advances and for which values oftime_point
advance at a steady rate relative to real time. That is, the clock may not be adjusted.
我注意到你的使用:
(void)m_condition.wait_until(guard, untilTime);
忽略 wait_until
的返回值。请注意,此函数可能虚假地返回。也就是说,它可能会返回,即使没有发出信号,即使超时尚未到期。
您可以发现超时是否已过期:
if (m_condition.wait_until(guard, untilTime) == std::cv_status::timeout)
...
可能(我猜)你的操作系统认为系统时间的调整是一个足够剧烈的事件,因此谨慎地向所有条件变量发出虚假信号是明智的。
关于linux - 在 Ubuntu 64 位上设置计算机时间后,计时器(使用 boost chrono steady_clock)到期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32135765/