在线文档教程
C++
线程支持 | Thread support

std::condition_variable::wait_until

性病:情况[医]变量::等待[医]直到

template< class Clock, class Duration > std::cv_status wait_until( std::unique_lock& lock, const std::chrono::time_point& timeout_time (1)(since C++11)
template< class Clock, class Duration, class Predicate > bool wait_until( std::unique_lock<std::mutex>& lock, const std::chrono::time_point<Clock, Duration>& timeout_time, Predicate pred (2)(since C++11)

wait_until导致当前线程阻塞,直到通知条件变量、到达特定时间或发生虚假唤醒为止,还可以选择循环直到满足某个谓词。

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

2%29相当于

二次

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

二次

此过载可用于忽略虚假唤醒。

调用此函数如果lock.mutex()不被当前线程锁定是未定义的行为。

调用此函数如果lock.mutex()与当前正在等待相同条件变量的所有其他线程使用的互斥对象不同,这是未定义的行为。

If these functions fail to meet the postcondition (lock.owns_lock()==true and lock.mutex() 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 std::unique_lock, which must be locked by the current thread
timeout_time-an object of type std::chrono::time_point representing the time when to stop 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指定的绝对超时。abs_time已经联系到了,std::cv_status::no_timeout太聪明了。

2%29false如果谓词pred仍然评估为false在...之后abs_time超时过期,否则为true如果超时已过期,则计算并返回pred...

例外

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

注记

钟系在timeout_time使用,这不需要是一个单调的时钟。如果时钟不连续地调整,则不能保证此函数的行为,但是现有的实现转换。timeout_timeClockstd::chrono::system_clock并委派给POSIX螺纹[医]康德[医]等待时间这样,等待就会尊重系统时钟,而不会尊重用户提供的时钟。Clock在任何情况下,该功能也可能等待更长的时间。timeout_time由于调度或资源争用延迟已到达。

即使使用中的时钟是std::chrono::steady_clock或者另一个单调的时钟,一个系统时钟的调整可能导致一个虚假的唤醒。

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

二次

#include <iostream> #include <atomic> #include <condition_variable> #include <thread> #include <chrono> using namespace std::chrono_literals; std::condition_variable cv; std::mutex cv_m; std::atomic<int> i{0}; void waits(int idx) { std::unique_lock<std::mutex> lk(cv_m auto now = std::chrono::system_clock::now( if(cv.wait_until(lk, now + 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 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( }

二次

可能的产出:

二次

Thread 1 timed out. i == 0 Notifying... Thread 2 timed out. i == 0 Notifying again... Thread 3 finished waiting. i == 1

二次

另见

waitblocks the current thread until the condition variable is woken up (public member function)
wait_forblocks the current thread until the condition variable is woken up or after the specified timeout duration (public member function)

c CND文件[医]等待时间

© cppreference.com

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

http://en.cppreference.com/w/cpp/线程/条件[医]可变/等待[医]直到