std::condition_variable通过“等待-通知”机制解决线程间协作问题,避免忙等。它需与std::mutex和std::unique_lock配合使用,wait()在条件不满足时释放锁并休眠,notify_one()/notify_all()唤醒等待线程,常用于生产者-消费者模型、线程池调度等场景,核心是避免虚假唤醒和丢失通知,确保高效安全的线程同步。

在C++多线程编程中,
std::condition_variable
要使用
std::condition_variable
std::mutex
std::unique_lock<std::mutex>
wait()
wait()
notify_one()
notify_all()
下面是一个经典的生产者-消费者模型示例,它清晰地展示了
std::condition_variable
#include <iostream>
#include <vector>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <chrono>
// 共享资源
std::mutex mtx; // 保护共享数据
std::condition_variable cv; // 条件变量
std::queue<int> data_queue; // 共享数据队列
bool stop_producing = false; // 停止生产的标志
void producer() {
for (int i = 0; i < 10; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 模拟生产耗时
{
std::unique_lock<std::mutex> lock(mtx); // 获取锁
data_queue.push(i); // 生产数据
std::cout << "Producer pushed: " << i << std::endl;
cv.notify_one(); // 通知一个等待的消费者
} // 锁在这里自动释放
}
// 生产完毕,通知所有消费者可以停止等待了
{
std::unique_lock<std::mutex> lock(mtx);
stop_producing = true; // 设置停止标志
std::cout << "Producer finished production, notifying all consumers." << std::endl;
} // 锁在这里自动释放
cv.notify_all(); // 唤醒所有等待的消费者
}
void consumer(int id) {
while (true) {
std::unique_lock<std::mutex> lock(mtx); // 获取锁
// 等待条件:队列不为空 或者 生产者已停止
// wait()函数会自动释放锁并休眠,被唤醒时会重新获取锁
cv.wait(lock, [&]{ return !data_queue.empty() || stop_producing; });
// 如果队列为空且生产者已停止,说明没有更多数据了,消费者可以退出了
if (data_queue.empty() && stop_producing) {
std::cout << "Consumer " << id << " finished." << std::endl;
break;
}
// 处理数据
int data = data_queue.front();
data_queue.pop();
std::cout << "Consumer " << id << " consumed: " << data << std::endl;
}
}
int main() {
std::thread prod_thread(producer);
std::thread cons_thread1(consumer, 1);
std::thread cons_thread2(consumer, 2); // 多个消费者
prod_thread.join();
cons_thread1.join();
cons_thread2.join();
std::cout << "All threads finished." << std::endl;
return 0;
}这段代码里,生产者线程在每次生产完数据后,会通过
cv.notify_one()
stop_producing
cv.notify_all()
cv.wait()
wait
wait
立即学习“C++免费学习笔记(深入)”;
std::condition_variable
说实话,在多线程编程里,光有互斥锁(
std::mutex
如果没有
std::condition_variable
// 糟糕的忙等示例
bool data_ready = false;
void consumer_bad() {
while (!data_ready) {
// 什么也不做,或者短暂休眠
std::this_thread::sleep_for(std::chrono::microseconds(1)); // 稍微好一点,但仍然是忙等
}
// 处理数据
}这种方式的弊端非常明显:它会白白消耗大量的CPU周期,即使条件不满足,线程也一直在运行,浪费资源。在实际项目中,这简直是性能杀手。
std::condition_variable
notify_all
在我看来,
std::condition_variable
std::condition_variable
wait()
notify()
理解
wait()
notify()
std::condition_variable
wait()
当一个线程调用
cv.wait(lock, predicate)
wait()
true
false
cv.wait(lock)
wait()
lock
std::unique_lock
notify_one()
notify_all()
wait()
true
wait()
false
notify
while
wait
notify_one()
notify_all()
当一个线程调用
notify_one()
notify_all()
notify_one()
notify_all()
何时使用notify_one()
notify_all()
notify_one()
notify_one()
notify_all()
stop_producing
notify_all()
选择正确的通知方式,既能保证程序的正确性,也能在一定程度上影响性能。
condition_variable
虽然
std::condition_variable
常见的陷阱:
std::mutex
data_queue
stop_producing
wait()
while
wait()
if (!condition) cv.wait(lock);
cv.wait(lock, [&]{ return condition; });while (!condition) { cv.wait(lock); }notify()
notify()
notify()
notify()
notify()
notify()
notify()
以上就是c++++如何使用条件变量_c++ condition_variable线程同步详解的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号