c++怎么使用条件变量std::condition_variable_c++多线程条件变量用法

std::condition_variable需与std::unique_lock配合使用,通过wait()等待条件并释放锁,由notify_one()或notify_all()唤醒线程,常用于生产者-消费者模型,需用谓词避免虚假唤醒,确保线程安全同步。

c++怎么使用条件变量std::condition_variable_c++多线程条件变量用法

c++多线程编程中,std::condition_variable 是一个重要的同步机制,用于在线程之间协调“等待-通知”操作。它通常与 std::unique_lock<:mutex> 配合使用,让某个线程等待特定条件成立,而另一个线程在条件达成时发出通知。

基本用法:等待与唤醒

std::condition_variable 的核心是两个方法:wait()notify_one()notify_all()

典型使用场景包括生产者-消费者模型、任务队列等。下面是一个简单的例子:

 #include <iostream> #include <thread> #include <mutex> #include <condition_variable>  std::mutex mtx; std::condition_variable cv; bool ready = false;  void worker_thread() {     std::unique_lock<std::mutex> lock(mtx);     // 等待直到 ready 为 true     cv.wait(lock, []{ return ready; });     std::cout << "工作线程开始执行任务n"; }  void main_thread() {     std::this_thread::sleep_for(std::chrono::seconds(1));     {         std::lock_guard<std::mutex> lock(mtx);         ready = true;     }     cv.notify_one(); // 唤醒一个等待的线程 }  int main() {     std::thread t1(worker_thread);     std::thread t2(main_thread);      t1.join();     t2.join();     return 0; } 

关键点说明

  • 必须配合互斥锁使用:condition_variable 的 wait 操作需要传入 unique_lock,不能单独使用。
  • wait 会自动释放锁:调用 wait 时,线程释放锁并进入阻塞状态;被唤醒后重新获取锁再继续执行。
  • 使用 Lambda 判断条件:推荐在 wait 第二个参数传入谓词(如 []{return ready;}),避免虚假唤醒导致的问题。
  • notify_one vs notify_all:notify_one() 唤醒一个等待线程,notify_all() 唤醒所有等待线程,根据场景选择。

生产者-消费者示例

更实用的例子:多个线程处理任务队列。

c++怎么使用条件变量std::condition_variable_c++多线程条件变量用法

商汤商量

商汤科技研发的AI对话工具,商量商量,都能解决。

c++怎么使用条件变量std::condition_variable_c++多线程条件变量用法 36

查看详情 c++怎么使用条件变量std::condition_variable_c++多线程条件变量用法

立即学习C++免费学习笔记(深入)”;

 #include <queue> #include <thread> #include <mutex> #include <condition_variable>  std::queue<int> tasks; std::mutex task_mutex; std::condition_variable task_cv; bool finished = false;  void consumer() {     while (true) {         std::unique_lock<std::mutex> lock(task_mutex);         // 等待任务或结束信号         task_cv.wait(lock, []{ return !tasks.empty() || finished; });          if (finished && tasks.empty()) {             lock.unlock();             break;         }          int task = tasks.front();         tasks.pop();         lock.unlock();          std::cout << "处理任务: " << task << "n";         std::this_thread::sleep_for(std::chrono::milliseconds(100));     } }  void producer() {     for (int i = 1; i <= 5; ++i) {         {             std::lock_guard<std::mutex> lock(task_mutex);             tasks.push(i);         }         task_cv.notify_one();         std::this_thread::sleep_for(std::chrono::milliseconds(50));     }      {         std::lock_guard<std::mutex> lock(task_mutex);         finished = true;     }     task_cv.notify_all(); // 唤醒所有消费者结束 }  int main() {     std::thread c(consumer);     std::thread p(producer);      p.join();     c.join();     return 0; } 

基本上就这些。掌握 condition_variable 的关键是理解它和互斥锁的协作方式,以及如何安全地传递状态变化。只要注意加锁、使用谓词等待、及时通知,就能写出可靠的多线程代码。

上一篇
下一篇
text=ZqhQzanResources