C++11 std::this_thread::sleep_until() 在使用 GCC 4.8.5 编译时挂起

C++11 std::this_thread::sleep_until() hangs when compiled with GCC 4.8.5

考虑以下程序:

#include <chrono>
#include <thread>

int main() {
    std::this_thread::sleep_until(std::chrono::steady_clock::now() - std::chrono::seconds(10));

    return 0;
}

使用 GCC 4.8.5 编译时,会挂起。用GCC 4.9及以上或clang3.4及以上编译时,立即returns,

为什么会挂起?据我了解,GCC 4.8.5 完全支持 C++11 标准。

这是一个已确认的错误,已在 gcc 4.9 中修复。

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58038

When using sleep_until() I get an bug with unsigned long scalar representations of a duration. If this duratoiin is in past, then you get an overflow in the length of the argument for sleep_for(). This causes an almost infinte sleep, instead of a fast return.

标准考虑了此案例,据此 sleep_until 应该继续处理。似乎是 GCC 4.8.5

中的错误

[33.2.4 Timing specifications]

The member functions whose names end in _until take an argument that specifies a time point. These functions produce absolute timeouts. Implementations should use the clock specified in the time point to measure time for these functions. Given a clock time point argument Ct, the clock time point of the return from timeout should be Ct + Di + Dm when the clock is not adjusted during the timeout. If the clock is adjusted to the time Ca during the timeout, the behavior should be as follows:

  • if Ca > Ct, the waiting function should wake as soon as possible, (...), since the timeout is already satisfied. [ Note: This specification may result in the total duration of the wait decreasing when measured against a steady clock. — end note ] — if Ca < Ct, ...