14

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?

8
  • Thank you for response. The question is because I caught this situation on a laptop when the system time was changed by NTP server. Only couple of minutes was enough for stopping all the process. The process is supposed to be active while the system is active. Well "NTP" is not a very usual situation, but I would like to avoid it.
    – Sergio Pv
    Nov 17, 2017 at 12:45
  • which compiler and what version?
    – Mgetz
    Nov 17, 2017 at 12:47
  • If 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. Nov 17, 2017 at 12:55
  • 3
    @SergioPv seems a bug, you've probably missed the 15.5 window but if you use the 'Report A Problem` button in the IDE there is a chance it'll get fixed for the next toolset update.
    – Mgetz
    Nov 17, 2017 at 13:07
  • 1
    @Mgetz: Sadly it's been reported many times; but can't fix it without a binary breaking release :( Nov 17, 2017 at 21:40

1 Answer 1

18

This was a known bug with VS2015's condition_variable which unfortunately is ABI-breaking to fix. I fixed it in our next major release (I completely rewrote the condition variable implementation from scratch), but I don't know when that will happen next. Note that VS2017 was a minor release as far as the standard libraries are concerned.

The problem is that the API contract exposed by msvcp140.dll expects an absolute time against the system clock, so if the system clock changes the effective wait length changes. The standard permits implementing an unsteady clock out of a steady clock but not the reverse; my predecessors apparently didn't get that memo :)

8
  • at least it's documented this is the case now, if you have a canonical VSO bug to link to so it can be tracked that would be awesome.
    – Mgetz
    Nov 18, 2017 at 14:29
  • @BillyONeal Thanks a lot. Could you give me some link to the bug. I just want to add it to the documentation as a reason to write own wrapper. Unfortunately a link to SO is not a reason for our PM :-/
    – Sergio Pv
    Nov 22, 2017 at 8:16
  • You could consider releasing a fixed .lib file though.
    – Joshua
    Jan 20, 2018 at 21:59
  • @Joshua: Nope, this thing lives in the DLL. We can't fix it in a static lib for the same reason we can't fix it in the headers. Jan 26, 2018 at 1:06
  • @BillyONeal: You know you can make static libraries to replace symbols in DLLs right?
    – Joshua
    Jan 26, 2018 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.