<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59260>59260</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            condition_variable::wait_for returns wrong value if timeout_duration is large
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          wAuner
      </td>
    </tr>
</table>

<pre>
    If the `timeout_duration` in the [condition_variable::wait_for](https://en.cppreference.com/w/cpp/thread/condition_variable/wait_for) member function is too big, the function always returns `std::cv_status::timeout`, even if no timeout occurred. Consider the following example ([Link to comparison in compiler explorer](https://godbolt.org/z/nsa5G9o5a)):

```c++
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <limits>
 
std::mutex m;
std::condition_variable cv;
std::string data;

bool processed = false;
 
using namespace std::chrono_literals;

void worker_thread()
{
    // Wait until main() sends data
    std::unique_lock lk(m);
    // changing int to long leads to wrong return value in std::condition_variable::wait_for
    auto longWait = std::numeric_limits<int>::max();
    auto status = cv.wait_for(lk, std::chrono::milliseconds{longWait});
 
    if (status == std::cv_status::timeout)
        std::cout << "timeout occurred" << std::endl;
    else if (status == std::cv_status::no_timeout)
        std::cout << "notification received, no timeout occurred" << std::endl;
}
 
int main()
{
    std::thread worker(worker_thread);
 
    std::this_thread::sleep_for(2s); // don't notify before thread can aquire lock
    std::cout << "notifying cv\n";
    cv.notify_one();
 
    worker.join();
}
```

As soon as the `longWait` timeout value takes on a 64bit value, the return value is wrong. Maybe an overflow?
This also seems to apply to GCC's libstdc++ implementation. Only MSVC's implementation delivers the correct and expected results.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVkuT4jYQ_jXm0hUX2DwPPuwyma1UZSuHpJIjJUsNaEeWvHrAkF-flmWbgWGytZQwWN3q_vqt2ohL9dse_BEhW069b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