Skip to content

condition_variable::wait_for doesn't wait if duration is large #46418

Open
@gnbond

Description

@gnbond
Bugzilla Link 47074
Version 10.0
OS All
Attachments Test cases with sample findings for various versions
CC @mclow

Extended Description

The following code should wait forever, but it does not, it returns immediately:

#include <chrono>
#include <iostream>
#include <condition_variable>

using namespace std::chrono;

int main() {
    std::mutex m;
    std::unique_lock l(m);
    std::condition_variable cv;

    std::cout << "sleeping forever" << std::endl;
    cv.wait_for(l, milliseconds::max(), []() { return false; });
    std::cout << "returned!" << std::endl;
}

Tested on FreeBSD, Ubuntu16 & MacOS, clang version 6, 8 & 10

Behaviour is somewhat variable, depending on OS and compiler version (at least for the small sample I have access to). Basically, given a duration past a certain size (somewhere around 1<<52 units of any duration type), wait_for() no longer waits at all. On some versions/platforms, the 2-argument wait_for() does not appear to wait at all, even for small durations.

This looks like some kind of overflow in the duration conversion.

Bug filed under libraries, but problem may be in the compiler intrinsics.

See attached somewhat more detailed test cases and notes on the limits I have discovered.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillalibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.threadingissues related to threading

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions