linux - 在 Ubuntu 64 位上设置计算机时间后,计时器(使用 boost chrono steady_clock)到期

标签 linux boost 64-bit c++-chrono

我们使用 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 of time_point never decrease as physical time advances and for which values of time_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/

相关文章:

linux - Linux中/etc/passwd文件最后一行获取用户ID

linux - 如何在 Manjaro 上将 sysvinit 脚本转换为 systemd

sharepoint - 32 位进程不支持 Microsoft SharePoint。请验证您是否在 64 位可执行文件中运行

c++ - 改变 boost::combine 的结果

.net - 系统.BadImageFormatException : Could not load file or assembly or one of its dependencies

c++ - 将 long int 转换为 void *

c - 将一个程序编译运行到另一个程序时出错

c++ - 从 linux 命令行用另一个替换整个段落

c++ - 如何克服 Boost Spirit AST 困惑

c++ - boost addable 和 dll 的辅助功能错误