首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::condition_variable_any::wait_for

template< class Lock, class Rep, class Period > std::cv_status wait_for( Lock& lock, const std::chrono::duration<Rep, Period>& rel_time);

(1)

(since C++11)

template< class Lock, class Rep, class Period, class Predicate > bool wait_for( Lock& lock, const std::chrono::duration<Rep, Period>& rel_time, Predicate pred);

(2)

(since C++11)

1%29原子释放lock,阻止当前正在执行的线程,并将其添加到等待执行的线程列表中。*this.线程将在notify_all()notify_one()执行,或者在相对超时时执行。rel_time过期了。它也可能是伪造的。不管原因是什么,lock重新获得和wait_for()出口。如果此函数通过异常退出,lock也被重新获得。%28直到C++14%29

2%29相当于return wait_until(lock, std::chrono::steady_clock::now() + rel_time, std::move(pred));这种过载可能被用来忽略虚假的唤醒。

用一个稳定的时钟来测量持续时间。此函数的阻塞时间可能超过timeout_duration由于调度或资源争用延迟。

If these functions fail to meet the postcondition (lock is locked by the calling thread), std::terminate is called. For example, this could happen if relocking the mutex throws an exception,

(since C++14)

参数

lock

-

an object of type Lock that meets the BasicLockable requirements, which must be locked by the current thread

rel_time

-

an object of type std::chrono::duration representing the maximum time to spend waiting

pred

-

predicate which returns ?false if the waiting should be continued. The signature of the predicate function should be equivalent to the following: bool pred();?

返回值

1%29std::cv_status::timeout指定的相对超时rel_time过期了,std::cv_status::no_timeout否则。

2%29false如果谓词pred仍然评估为false在...之后rel_time超时过期,否则为true...

例外

1%29

May throw std::system_error, may also propagate exceptions thrown by lock.lock() or lock.unlock().

(until C++14)

Any exception thrown by clock, time_point, or duration during the execution (clocks, time points, and durations provided by the standard library never throw).

(since C++14)

2%29与%281%29相同,但也可能传播由pred

注记

即使在锁定下通知,重载%281%29也不能保证在由于超时而返回时关联谓词的状态。

对…的影响notify_one()/notify_all()三个原子部分wait()/wait_for()/wait_until()%28解锁+等待、唤醒和锁定%29按一个总顺序进行,可视为修改顺序原子变量的顺序是特定于这个单独的条件的。[医]变量。这使得不可能notify_one(),例如,延迟并取消阻塞在调用之后才开始等待的线程。notify_one()被制造出来了。

二次

代码语言:javascript
复制
#include <iostream>
#include <atomic>
#include <condition_variable>
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
 
std::condition_variable_any cv;
std::mutex cv_m;
int i;
 
void waits(int idx)
{
    std::unique_lock<std::mutex> lk(cv_m);
    if(cv.wait_for(lk, idx*100ms, []{return i == 1;})) 
        std::cerr << "Thread " << idx << " finished waiting. i == " << i << '\n';
    else
        std::cerr << "Thread " << idx << " timed out. i == " << i << '\n';
}
 
void signals()
{
    std::this_thread::sleep_for(120ms);
    std::cerr << "Notifying...\n";
    cv.notify_all();
    std::this_thread::sleep_for(100ms);
    {
        std::lock_guard<std::mutex> lk(cv_m);
        i = 1;
    }
    std::cerr << "Notifying again...\n";
    cv.notify_all();
}
 
int main()
{
    std::thread t1(waits, 1), t2(waits, 2), t3(waits, 3), t4(signals);
    t1.join(); t2.join(), t3.join(), t4.join();
}

二次

产出:

二次

代码语言:javascript
复制
Thread 1 timed out. i == 0
Notifying...
Thread 2 timed out. i == 0
Notifying again...
Thread 3 finished waiting. i == 1

二次

另见

wait

blocks the current thread until the condition variable is woken up (public member function)

wait_until

blocks the current thread until the condition variable is woken up or until specified time point has been reached (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com