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

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
#include <thread>
#include <condition_variable>
#include <chrono>
#include <cstdlib>

using namespace std::chrono;

int main()
{
  std::mutex mx;
  std::condition_variable cv;
  std::thread sleepy_abs([&] {
    time_point<system_clock, minutes> end_of_time(minutes::max());
    std::unique_lock<std::mutex> lk(mx);
    // Rather than overflowing to a negative value, the timeout should be
    // truncated to seconds::max() and so sleep for 292 billion years.
    cv.wait_until(lk, end_of_time);
    // This should not happen:
    throw 1;
  });
  std::thread sleepy_rel([&] {
    std::unique_lock<std::mutex> lk(mx);
    // Rather than overflowing to a negative value, the timeout should be
    // truncated to seconds::max() and so sleep for 292 billion years.
    cv.wait_for(lk, minutes::max());
    // This should not happen:
    throw 1;
  });
  // Give the new threads a chance to start sleeping:
  std::this_thread::yield();
  std::this_thread::sleep_for(seconds(2));
  // If we get here without the other threads throwing an exception
  // then it should be sleeping peacefully, so the test passed.
  std::_Exit(0);
}

Reply via email to