<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/148328>148328</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
std::condition_variable and std::condition_variable_any
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
johnsheu
</td>
</tr>
</table>
<pre>
`wait_for()` and `wait_until()` for `std::condition_variable` and `std::condition_variable_any` take their wait duration or timeout parameters by const-reference. This can lead to unexpected behavior if the referent changes while the `wait_for()` or `wait_until()` blocks.
For example:
```c++
template <class _Clock, class _Duration>
_LIBCPP_HIDE_FROM_ABI cv_status
wait_until(unique_lock<mutex>& __lk, const chrono::time_point<_Clock, _Duration>& __t) {
using namespace chrono;
using __clock_tp_ns = time_point<_Clock, nanoseconds>;
typename _Clock::time_point __now = _Clock::now();
if (__t <= __now)
return cv_status::timeout;
__clock_tp_ns __t_ns = __clock_tp_ns(std::__safe_nanosecond_cast(__t.time_since_epoch()));
__do_timed_wait(__lk, __t_ns);
return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout;
}
```
The duration parameter `__t` is accessed by-reference, and so its value may be different before and after the wait.
For `std::condition_variable_any`, this is an actual data race if the timeout parameter is protected by the lock, as the implementation accesses the timeout outside of the lock.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVU2P4zYM_TXKhdjAkePYOfiQSTboAC26KPYuyDIda9eWXIlKJv--kO3Jx3Y6KCAgCEg9vvco0tJ7fTKIJcteWHZYyECtdeUP2xrfYlhUtr6WbJNcpCbRWMd4wfiWbRKQpob3QDCku3uosS6GPNUs3bF0p6ypNWlrxFk6LasOHwA-yRLSXGMmyZ8I1KJ2EMtBHZyMiWAdkO7RBoJBOtkjofNQXUFZ4-mLwwYdGoVLgO-t9qCkgQ5lDWQhGHwbUBHWUGErz9o60E0sA_M9AtVKc0IPl1Z3IwP40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