当前位置: 首页 > news >正文

std::condition_variable

C++ 11 提供了std::condition_variable这个类代表条件变量,与 Linux 系统原生的条件变量一样,同时提供了等待条件变量满足的wait系列方法(wait、wait_for、wait_until 方法),发送条件信号使用notify方法(notify_onenotify_all方法),当然使用std::condition_variable对象时需要绑定一个std::unique_lockstd::lock_guard对象。

C++ 11 中 std::condition_variable 不再需要显式调用方法初始化和销毁。

我们将上文中介绍 Linux 条件变量的例子改写成 C++ 11 版本:

#include <thread> #include <mutex> #include <condition_variable> #include <list> #include <iostream> class Task { public: Task(int taskID) { this->taskID = taskID; } void doTask() { std::cout << "handle a task, taskID: " << taskID << ", threadID: " << std::this_thread::get_id() << std::endl; } private: int taskID; }; std::mutex mymutex; std::list<Task*> tasks; std::condition_variable mycv; void* consumer_thread() { Task* pTask = NULL; while (true) { std::unique_lock<std::mutex> guard(mymutex); while (tasks.empty()) { //如果获得了互斥锁,但是条件不合适的话,pthread_cond_wait会释放锁,不往下执行。 //当发生变化后,条件合适,pthread_cond_wait将直接获得锁。 mycv.wait(guard); } pTask = tasks.front(); tasks.pop_front(); if (pTask == NULL) continue; pTask->doTask(); delete pTask; pTask = NULL; } return NULL; } void* producer_thread() { int taskID = 0; Task* pTask = NULL; while (true) { pTask = new Task(taskID); //使用括号减小guard锁的作用范围 { std::lock_guard<std::mutex> guard(mymutex); tasks.push_back(pTask); std::cout << "produce a task, taskID: " << taskID << ", threadID: " << std::this_thread::get_id() << std::endl; } //释放信号量,通知消费者线程 mycv.notify_one(); taskID ++; //休眠1秒 std::this_thread::sleep_for(std::chrono::seconds(1)); } return NULL; } int main() { //创建5个消费者线程 std::thread consumer1(consumer_thread); std::thread consumer2(consumer_thread); std::thread consumer3(consumer_thread); std::thread consumer4(consumer_thread); std::thread consumer5(consumer_thread); //创建一个生产者线程 std::thread producer(producer_thread); producer.join(); consumer1.join(); consumer2.join(); consumer3.join(); consumer4.join(); consumer5.join(); return 0; }

编译并执行程序输出结果如下所示:

[root@localhost testmultithread]# g++ -g -o cpp11cv cpp11cv.cpp -std=c++0x -lpthread [root@localhost testmultithread]# ./cpp11cv produce a task, taskID: 0, threadID: 140427590100736 handle a task, taskID: 0, threadID: 140427623671552 produce a task, taskID: 1, threadID: 140427590100736 handle a task, taskID: 1, threadID: 140427632064256 produce a task, taskID: 2, threadID: 140427590100736 handle a task, taskID: 2, threadID: 140427615278848 produce a task, taskID: 3, threadID: 140427590100736 handle a task, taskID: 3, threadID: 140427606886144 produce a task, taskID: 4, threadID: 140427590100736 handle a task, taskID: 4, threadID: 140427598493440 produce a task, taskID: 5, threadID: 140427590100736 handle a task, taskID: 5, threadID: 140427623671552 produce a task, taskID: 6, threadID: 140427590100736 handle a task, taskID: 6, threadID: 140427632064256 produce a task, taskID: 7, threadID: 140427590100736 handle a task, taskID: 7, threadID: 140427615278848 produce a task, taskID: 8, threadID: 140427590100736 handle a task, taskID: 8, threadID: 140427606886144 produce a task, taskID: 9, threadID: 140427590100736 handle a task, taskID: 9, threadID: 140427598493440 ...更多输出结果省略...

需要注意的是,如果在 Linux 平台上,std::condition_variable 也存在虚假唤醒这一现象,如何避免与上文中介绍 Linux 原生的条件变量方法一样。

http://www.jsqmd.com/news/1105533/

相关文章:

  • STM32F745ZG与TPS65263的嵌入式电源管理设计
  • Postman接口测试实战:从单接口调试到业务流程自动化
  • .NET MAUI跨平台UI自动化测试实战:Appium环境搭建与POM设计
  • LLM原生工具调用与记忆能力如何消解Agent中间层
  • 上下文工程:构建大模型稳定交互的认知框架
  • SMUDebugTool完整指南:解锁AMD Ryzen处理器性能潜力的终极免费工具
  • Claude v4语义压缩层蒸发:从可控推理到确定性工程的范式迁移
  • Anthropic Claude模型能力演进与安全发布实践解析
  • Selenium登录界面自动化测试:从环境搭建到框架设计的完整实践指南
  • 大模型MoE架构揭秘:稀疏激活如何让1.8万亿参数仅用2%?
  • Playwright设备模拟实战:从原理到配置,解决跨端测试环境脱节问题
  • 终极指南:5步搞定macOS Navicat Premium 17.x试用期无限重置
  • AI视觉驱动自动化测试:Midscene.js原理、实践与CI/CD集成指南
  • Claude零层架构解析:语义保真度校验环的降维重构
  • DeepSeek-V2工程解析:动态注意力与多跳记忆的高效推理实践
  • 铜钟音乐:终极免费纯净听歌平台完整使用指南 [特殊字符]
  • DSPy Few-Shot Optimization:可编程示例优化原理与生产实践
  • Mythos大模型能力跃迁与门控释放机制解析
  • BLAST:面向LLM的高性能浏览器增强架构
  • [智能体-628]:OpenClaw可以建立多个channel吗?
  • NLP工程师十年实录:从正则到大模型的工程演进
  • MAA明日方舟自动化助手技术指南:图像识别驱动的智能任务管理方案
  • NLP工程师的语义脉搏监测系统:News Cypher设计原理与实操框架
  • Claude语义蒸馏层消失:中间态可解释性终结与架构重构指南
  • Selenium自动化测试入门:从环境搭建到实战避坑指南
  • Anthropic上下文编排层‘归零’:RAG范式迁移与工程重构
  • 三步解锁Axure RP中文界面:从英文困扰到流畅设计的完整方案
  • 基于PIC18F46K20的无刷电机FOC控制实现与优化
  • Qwen3开源大模型产品化实践:MoE架构与双模式推理深度解析
  • GPT-Builder+Plotly地理可视化智能体构建范式