7

Consider the following program:

#include <chrono>
#include <thread>

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

    return 0;
}

When compiled with GCC 4.8.5, it will hang. When compiled with GCC 4.9 and above or clang3.4 and above, it returns immediately,

Why would it hang? As I understand, GCC 4.8.5 fully supports C++11 standard.

3
  • 2
    what do you think std::chrono::steady_clock::now() represents? hint: subtracting 10 from that number will not change much
    – Fureeish
    Dec 20, 2017 at 22:15
  • @Fureeish they're probably asking if something in the standard changed that affects the behavior of sleep_until. Dec 20, 2017 at 22:20
  • @Fureeish Question edited. It was unclear on what is the expected behavior. Dec 21, 2017 at 7:25

2 Answers 2

7

This is a confirmed bug that was fixed in 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.

5

This case is considered by the Standard and according to it sleep_until should move on. Seems its a bug in 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, ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.