C++ std::thread 简单的生产者和消费者
来源:互联网 发布:ipad pro app 知乎 编辑:程序博客网 时间:2024/06/06 15:35
#include <iostream>#include <thread>#include <mutex>#include <deque>#include <vector>#include <unistd.h>std::deque<int> data;std::mutex m_mux;//全局互斥锁int num=0;void product_thread(){ while(true){ std::unique_lock<std::mutex> lck(m_mux); num = ++num % 1000; data.push_front(num); printf("product %d\n", num); lck.unlock(); sleep(2); }}void consume_thread(){ while (true){ std::unique_lock<std::mutex> lck(m_mux); if(data.empty()) { lck.unlock(); continue; } printf("consume %d\n", data.back()); data.pop_back(); lck.unlock(); sleep(5); }}int main() { std::thread thread_product(&product_thread); std::cout<<"Now it runs after the thread_product function !"<<std::endl; std::thread thread (&consume_thread); std::cout<<"Now it runs after the thread function !"<<std::endl; thread_product.join(); std::cout<<"Now it runs after the thread_product function !"<<std::endl; thread.join(); std::cout<<"Now it runs after the thread.join function !"<<std::endl; return 0;}
0 0
- C++ std::thread 简单的生产者和消费者
- 多线程Thread生产者和消费者的问题
- Thread的生产者和消费者模式
- 生产者-消费者 C的简单模拟
- 生产者和消费者的简单例子
- 简单的activemq,生产者和消费者代码
- 简单的生产者-消费者
- 一个简单的C语言操作系统生产者消费者模型
- 简单的生产者消费者模型
- 一个简单的生产者消费者
- 简单的生产者消费者例子
- Thread生产者消费者例子
- 生产者和消费者的简单的例子:用互斥锁控制
- (转)一段生产者和消费者的简单多线程代码
- Java中简单的生产者和消费者问题
- 使用线程完成简单的生产者和消费者
- linux进程线程下简单的消费者和生产者
- ACE_Task的生产者和消费者
- Manacher算法:求解最长回文字符串,时间复杂度为O(N)
- Matlab车牌识别系统
- 经典查找算法
- chardet==>探别编码问题
- Android Animation
- C++ std::thread 简单的生产者和消费者
- leetcode [House Robber]
- 正则
- vector 容器 用法详解
- 算法导论 基数排序
- 解决JDK版本问题
- 关于Vuex,官方文档笔记
- Allegro铺铜详解
- Java垃圾回收机制