简体   繁体   English

通知 cv 时 condition_variable::wait_for 是否返回 true

[英]Does condition_variable::wait_for return true when cv is notified

here is a small code snippet这是一个小代码片段

request_ptr pop()
{
    request_ptr rp;

    std::unique_lock<std::mutex> lock(cv_mtx);
    auto time = std::chrono::milliseconds(10000);
    if (!cv.wait_for(lock, time, [this] { return !this->is_empty(); }))
    {
        return rp;
    }

    std::lock_guard<std::mutex> mtx_guard(mtx);
    rp.reset(new request(std::move(requests.front())));
    requests.pop();
    return rp;
}

How does wait_for behave when cv is notified?通知cvwait_for的行为如何? Does it return true without check of returned value of is_empty ?它是否返回true而不检查is_empty的返回值? Or it returns true only if is_empty returns true ?还是仅当is_empty返回true时才返回true

According to [thread.condition.condvar]/36 the call...根据[thread.condition.condvar]/36调用...

cv.wait_for(lock, rel_time, pred);

is equivalent to...相当于...

cv.wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));

which is, in turn , equivalent to...反过来,这相当于...

while (!pred())
    if (wait_until(lock, abs_time) == cv_status::timeout)
        return pred();
return true;

So the value returned by wait_for should be the current value as returned by the predicate.所以wait_for返回的值应该是谓词返回的当前值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM