[libcxx-commits] [libcxx] [libc++] Save duration/timeout locally for condition_variable waits (PR #148330)

John Sheu via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jul 12 03:53:07 PDT 2025


johnsheu wrote:

Consider this program.

1.  Is it well-formed?
2.  What should it output?

```c++
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <latch>
#include <mutex>
#include <thread>

int main(int argc, char** argv) {
  std::mutex mutex;
  std::condition_variable cv;
  std::latch latch(1);
  auto timeout = std::chrono::steady_clock::time_point::max();

  std::thread thread1([&](){
    std::unique_lock lock(mutex);
    latch.count_down();

    const auto status = cv.wait_until(lock, timeout);
    switch (status) {
      case std::cv_status::no_timeout:
        std::cout << "no_timeout" << std::endl;
        break;
      case std::cv_status::timeout:
        std::cout << "timeout" << std::endl;
        break;
    }
  });

  std::thread thread2([&](){
    latch.wait();
    std::unique_lock lock(mutex);

    cv.notify_one();
    timeout = std::chrono::steady_clock::time_point::min();
  });

  thread1.join();
  thread2.join();
  return 0;
}
```

https://github.com/llvm/llvm-project/pull/148330


More information about the libcxx-commits mailing list