Namespaces
Variants

std::condition_variable:: wait_for

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
template < class Rep, class Period >

std:: cv_status wait_for ( std:: unique_lock < std:: mutex > & lock,

const std:: chrono :: duration < Rep, Period > & rel_time ) ;
(1) (自 C++11 起)
template < class Rep, class Period, class Predicate >

bool wait_for ( std:: unique_lock < std:: mutex > & lock,
const std:: chrono :: duration < Rep, Period > & rel_time,

Predicate pred ) ;
(2) (自 C++11 起)

wait_for 会使当前线程阻塞,直到条件变量被通知、指定的持续时间已过,或发生伪唤醒。可选择性地提供 pred 来检测伪唤醒。

1) 等价于 return wait_until ( lock, std:: chrono :: steady_clock :: now ( ) + rel_time ) ;
2) 等价于 return wait_until ( lock, std:: chrono :: steady_clock :: now ( ) + rel_time, std :: move ( pred ) ) ;
该重载可用于在等待特定条件变为真时忽略伪唤醒。

wait_for 返回后, lock. owns_lock ( ) true ,且 lock. mutex ( ) 已被调用线程锁定。若无法满足这些后置条件 [1] ,将调用 std::terminate

若满足以下任一条件,则行为未定义:

  • lock. owns_lock ( ) false
  • lock. mutex ( ) 未被调用线程锁定。
  • 若其他线程也在等待 * this ,则 lock. mutex ( ) 与这些线程通过等待函数( wait wait_for wait_until )在 * this 上解锁的互斥量不同。
  1. 这种情况可能发生在互斥锁重新加锁时抛出异常的情况下。

目录

参数

lock - 必须由调用线程持有的锁
rel_time - 等待的最大持续时间
pred - 用于检查等待是否可完成的谓词
类型要求
-
Predicate 必须满足 FunctionObject 的要求。
-
pred ( ) 必须是合法表达式,且其类型和值类别必须满足 BooleanTestable 要求。

返回值

1) std:: cv_status :: timeout 若自本次调用开始经过的时间已达到 rel_time ,否则返回 std:: cv_status :: no_timeout
2) 返回调用方前 pred ( ) 的最新结果。

异常

1) 超时相关异常。
2) 与超时相关的异常,以及由 pred 抛出的任何异常。

注释

即使是在锁下被通知,重载 (1) 对于因超时返回时关联谓词的状态不作任何保证。

notify_one() / notify_all() 的效果以及 wait() / wait_for() / wait_until() 的三个原子操作部分(解锁+等待、唤醒和加锁)的发生顺序构成一个单一的全序,可视为原子变量的 修改顺序 :该顺序特定于此条件变量。这使得例如 notify_one() 不可能被延迟,并解除那些恰好在 notify_one() 调用之后才开始等待的线程的阻塞状态。

示例

#include <chrono>
#include <condition_variable>
#include <iostream>
#include <thread>
std::condition_variable cv;
std::mutex cv_m; // 此互斥锁用于三个目的:
                 // 1) 同步对 i 的访问
                 // 2) 同步对 std::cerr 的访问
                 // 3) 用于条件变量 cv
int i = 0;
void waits()
{
    std::unique_lock<std::mutex> lk(cv_m);
    std::cerr << "Waiting... \n";
    cv.wait(lk, []{ return i == 1; });
    std::cerr << "...finished waiting. i == 1\n";
}
void signals()
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
    {
        std::lock_guard<std::mutex> lk(cv_m);
        std::cerr << "Notifying...\n";
    }
    cv.notify_all();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    {
        std::lock_guard<std::mutex> lk(cv_m);
        i = 1;
        std::cerr << "Notifying again...\n";
    }
    cv.notify_all();
}
int main()
{
    std::thread t1(waits), t2(waits), t3(waits), t4(signals);
    t1.join(); 
    t2.join(); 
    t3.join();
    t4.join();
}

可能的输出:

Waiting...
Waiting...
Waiting...
Notifying...
Notifying again...
...finished waiting. i == 1
...finished waiting. i == 1
...finished waiting. i == 1

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 应用于 发布时的行为 正确行为
LWG 2093 C++11 规范中缺少超时相关异常说明 明确提及这些异常
LWG 2114
( P2167R3 )
C++11 转换为 bool 的能力过弱,无法满足实现预期 强化了相关要求
LWG 2135 C++11 lock. lock ( ) 抛出异常时的行为不明确 在此情况下调用 std::terminate

参见

阻塞当前线程,直至条件变量被唤醒
(公开成员函数)
阻塞当前线程,直至条件变量被唤醒或到达指定时间点
(公开成员函数)