I'm trying to use wait_for function of std::condition_variable compiled with Visual C++ 2017 however it returns random unexpected results with the following simple code (which never signals the variable, so it's supposed to always return timeout):
condition_variable m_cv;
mutex m_mutex;
void ThreadProc()
{
while (1)
{
unique_lock<mutex> lk(m_mutex);
cv_status r = m_cv.wait_for(lk, chrono::milliseconds(200));
printf("Result %s\n", r == cv_status::timeout ? "timeout" : "no timeout");
Sleep(100);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
thread th(ThreadProc);
th.join();
return 0;
}
The results are random like:
Result no timeout
Result timeout
Result no timeout
Result timeout
Result timeout
Result timeout
Result timeout
Result no timeout
Am I doing something wrong or this is a compiler bug?
200milliseconds. Usewait_untilif you want accurate regular wake-up times (still checking for spurious events).