Open cpplearner opened 2 weeks ago
~The round-up in _Clamped_rel_time_ms_count
(introduced in #4457) looks like the reason. 249758μs would be turned into 250ms here.~
CC @AlexGuteniev.
The round-up in
_Clamped_rel_time_ms_count
(introduced in #4457) looks like the reason
Why? It rounds the parameter passed to wait, so would err on the other side.
The round-up in
_Clamped_rel_time_ms_count
(introduced in #4457) looks like the reasonWhy? It rounds the parameter passed to wait, so would err on the other side.
Oh, I see. Probably I was wrong then.
Can we reproduce the error with SleepConditionVariableSRW
? If so, we may need to work around it.
This looks like an STL bug that wasn't fixed by #4457 (despite how I Changelogged it, oopsy).
According to WG21-N4981 [thread.condvarany.wait]/11:
template<class Lock, class Rep, class Period> cv_status wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time);
Effects: Equivalent to:
return wait_until(lock, chrono::steady_clock::now() + rel_time);
condition_variable_any::wait_for
should use the steady_clock
. But we don't do that:
https://github.com/microsoft/STL/blob/e36ee6c2b9bc6f5b1f70776c18cf5d3a93a69798/stl/inc/condition_variable#L117-L126 https://github.com/microsoft/STL/blob/e36ee6c2b9bc6f5b1f70776c18cf5d3a93a69798/stl/inc/condition_variable#L227-L233 https://github.com/microsoft/STL/blob/e36ee6c2b9bc6f5b1f70776c18cf5d3a93a69798/stl/src/sharedmutex.cpp#L44-L54
We're calling GetTickCount64()
which is the system clock.
We need to audit this area again (both condition_variable
and condition_variable_any
) and make sure we Do What The Standard SaysTM.
@AlexGuteniev @frederick-vs-ja @StephanTLavavej @cpplearner I would like to collaborate on this, requested in Discord chat space too :)
We're calling
GetTickCount64()
which is the system clock.
Actually it is not a system clock, it is a steady clock, but an imprecise steady clock not matching std::steady_clock
.
Describe the bug
Revealed by libc++ test
test/std/thread/thread.condition/thread.condition.condvarany/wait_for.pass.cpp
.[thread.condvarany.wait]/13:
But MSVC STL's
condition_variable_any::wait_for
sometimes returnscv_status::timeout
even though the elapsed time (measured byhigh_resolution_clock
) is shorter than the requested timeout.Command-line test case
(You might need to execute the program several times to see the output.)
Expected behavior
.\test-condvarany.exe
should consistently produce no outputSTL version