条件变量:并发编程中的一种同步原语,允许线程在某个条件尚未满足时进入等待状态,并在其他线程发出通知(signal/broadcast)后被唤醒,通常与互斥锁(mutex)配合使用,以安全地检查与更新共享状态。(也常简称 condvar)
The worker thread waits on a condition variable until there is a new task.
工作线程在条件变量上等待,直到有新的任务到来。
To avoid busy-waiting, the program uses a mutex plus a condition variable so that consumers sleep when the queue is empty and wake up when producers enqueue items.
为避免忙等,程序使用互斥锁配合条件变量:当队列为空时消费者休眠,当生产者入队时再将其唤醒。
/kənˈdɪʃən ˈvɛriəbəl/
Condition 来自拉丁语 condicio(“条件、约定”),variable 来自拉丁语 variabilis(“可变化的”)。合起来在计算机并发语境中表示:线程等待的是某个“条件(状态)”,而该条件会随程序运行而“变化”,因此用一种机制来在条件变化时通知等待者。
std::condition_variable)有关的同步思想。