Well. I have Win10 x64 and this pretty simple code:
int main() {
std::conditional_variable cv;
std::mutex m;
std::unique_lock<std::mutex> lock(m);
while(1) {
cv.wait_for(lock, 1000ms, [](){return false;});
std::cout << "!" << std::endl;
}
}
Yes. The code works just like I expect; It shows '!' every second.
But If I change my local time (minus 1 hour/minute e.g.) it holds forever.
If I replace cv.wait_for(...)
with WinAPI Sleep(1000)
it works fine. Also WinAPI SleepConditionVariableCS
works fine.
I have tried this:
cv.wait_until(lock, std::chrono::steady_clock::now() + 1000ms, [](){return false;});
And it gives me the same result.
BTW: The behavior of the calls like std::this_thread::sleep_for
, std::this_thread::sleep_until
is the same.
So the question is: Is it possible to use std::condition_variable
with expected behavior? Or I just have to replace it with my own CONDITION_VARIABLE wrapper?
std::steady_clock
jumps, especially backwards, then it's wrong. It's should be a monotonic clock, it should advance only forward, and do it in a steady pace. It should not be adjusted.