简体   繁体   中英

How works std::condition_variable::wait_until

Why this code works very fast?

int main() {
    std::condition_variable cv;
    std::mutex mtx;
    std::unique_lock<std::mutex> lock(mtx);
    cv.wait_until(lock, std::chrono::system_clock::now() + 10000ms);

    return 0;
}

Seems like wait_until ignored

You're probably getting a spurious wakeup . Try using the overload that accepts a predicate...

#include <condition_variable>

int main() {
    std::condition_variable cv;
    std::mutex mtx;
    std::unique_lock<std::mutex> lock(mtx);
    cv.wait_until(lock, std::chrono::system_clock::now() + 10000ms, []{ return false; });

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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