我尝试使用用{C} 2017编译的wait_for
std::condition_variable
函数,但是它使用以下简单代码返回随机意外结果(它从不向变量发出信号,因此它&#39} ; s应该总是返回超时):
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;
}
结果是随机的:
Result no timeout
Result timeout
Result no timeout
Result timeout
Result timeout
Result timeout
Result timeout
Result no timeout
我做错了什么或这是编译错误?