[英]0 as a timeout in std::condition_variable::wait_for
For instance I have next code:例如我有下一个代码:
void waitForResponse(const std::optional<int64_t>& ms){
std::unique_lock lk{_mtx};
_cv.wait_for(lk, std::chrono::milliseconds{ms ? *ms : 0}, []() {
return someCondition;
}
}
Is it specified in standard if I pass 0
as a duration argument?如果我将
0
作为持续时间参数传递,它是否在标准中指定? Is it equal to the next code?:是否等于下一个代码?:
void waitForResponse(const std::optional<int64_t>& ms){
std::unique_lock lk{_mtx};
_cv.wait_for(lk, []() {
return someCondition;
}
}
Is there any overhead?有开销吗?
According to the working draft and C++11 standard section32.6.3 (int the working draft), wait_for
is根据工作草案和 C++11 标准第32.6.3节(工作草案),
wait_for
是
Equivalent to:
return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));
等价于:
return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));
So when you are passing所以当你经过
_cv.wait_for(lk, std::chrono::milliseconds{0}, []() return someCondition;}
you are basically calling你基本上是在打电话
_cv.wait_until(lk, chrono::steady_clock::now(), []() return someCondition;}
and according to the timing specifications , the function times out when chrono::steady_clock::now()
> C_t (C_t the time point passed to the wait_until
function), which will timeout (and thus return) basically immediately.并且根据时序规范,function 在 chrono
chrono::steady_clock::now()
> C_t (C_t 传递给wait_until
函数的时间点)时超时,基本上会立即超时(并因此返回)。
And so it is different to所以它是不同的
_cv.wait(lk, []() {
return someCondition;
}
which will block until someCondition
is true.这将阻塞直到
someCondition
为真。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.