C++怎么使用条件变量std::condition_variable_C++线程通信与同步

std::condition_variable用于线程同步,配合互斥量和条件判断实现等待/通知机制。其核心是wait()、notify_one()和notify_all(),常用于生产者-消费者模型。使用时需注意虚假唤醒、锁类型选择及唤醒时机,确保共享数据一致性与线程协作效率。

在C++多线程编程中,std::condition_variable 是实现线程间通信与同步的重要工具。它通常配合 std::unique_lock<:mutex> 使用,用于阻塞一个或多个线程,直到另一个线程通知它们条件已满足。

1. 条件变量的基本组成

使用 std::condition_variable 需要三个关键元素:

  • 互斥量(std::mutex):保护共享数据,防止竞态条件。
  • 条件变量(std::condition_variable):用于线程等待或唤醒。
  • 共享状态的判断条件:决定是否继续执行或等待。

2. 常用成员函数说明

wait():让当前线程阻塞,直到被其他线程唤醒。

  • cond.wait(lock):释放锁并进入等待状态;被唤醒后自动重新获取锁。
  • cond.wait(lock, predicate):等待直到谓词(判断条件)为 true,更安全推荐使用。

notify_one():唤醒一个正在等待的线程。

notify_all():唤醒所有等待中的线程。

3. 简单示例:生产者-消费者模型

下面是一个使用 std::condition_variable 实现的简单生产者-消费者例子:

#include 
#include 
#include 
#include 
#include 

std::queue data_queue;
std::mutex mtx;
std::condition_variable cond;

void producer() {
    for (int i = 0; i < 5; ++i) {
        std::lock_guard lock(mtx);
        data_queue.push(i);
        std::cout << "生产: " << i << "\n";
        cond.notify_one();  // 通知消费者
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

void consumer() {
    while (true) {
        std::unique_lock lock(mtx);
        // 等待队列非空
        cond.wait(lock, []{ return !data_queue.empty(); });

        int value = data_queue.front();
        data_queue.pop();
        std::cout << "消费: " << value << "\n";

        if (value == 4) break;  // 结束信号
    }
}

int main() {
    std::thread c(consumer);
    std::thread p(producer);

    p.join();
    c.join();

    return 0;
}

在这个例子中:

  • 生产者向队列添加数据,并调用 notify_one() 唤醒消费者。
  • 消费者调用 wait() 等待队列非空,避免忙等。
  • 使用 std::unique_lock 是因为 wait() 要求锁能被临时释放和重新获取。

4. 使用注意事项

  • 必须配合互斥量使用wait() 内部会释放锁,所以不能传入 std::lock_guard 或普通锁。
  • 避免虚假唤醒:即使没有调用 notify,等待也可能结束。因此建议使用带谓词的 wait 形式。
  • notify 后数据应有效:确保唤醒时共享数据已处于一致状态。
  • 选择 notify_one 还是 notify_all:如果只有一个线程需要处理,用 notify_one 更高效。
基本上就这些。合理使用 std::condition_variable 可以实现高效的线程协作,但要注意锁的粒度和等待条件的正确性。