用BlockBoundQueue和c++11实现多线程生产者消费者问题
// file : blockBoundQueue.h
#ifndef YANG_BLOCKBOUNDQUEUE
#define YANG_BLOCKBOUNDQUEUE
#include <mutex>
#include <condition_variable>
#include <queue>
#include <cstdio>
namespace yang{
#define _FULL 4 //
template <typename _Tp>
class BlockBoundQueue
{
public:
BlockBoundQueue(size_t bound = _FULL) :bound_(bound){}
BlockBoundQueue(const BlockBoundQueue&) = delete;
BlockBoundQueue& operator=(const BlockBoundQueue&) = delete;
void push(const _Tp& value)
{
std::unique_lock<std::mutex> lck(mutex_);// 利用RAII技法,将mutex_托管给lck
while (count_ + 1 == bound_)// 用while防止虚假唤醒
{
printf("the queue is full , waiting for the consumer consuming !\n");
notFull_.wait(lck); //等待队列非满条件发生
}
count_++;
queue_.push(value);
notEmpty_.notify_one();//通知队列非空,不能用all,读者自行思考为什么
}
_Tp get()
{
std::unique_lock<std::mutex> lck(mutex_);
while (queue_.empty())
{
printf("the queue is empty , waiting for the producer producing !\n");
notEmpty_.wait(lck);//等待队列为非空
}
_Tp front(queue_.front());
queue_.pop();
count_--;
notFull_.notify_one();//通知队列为非满,请注意不能用all
return front;
}
private:
std::mutex mutex_;
std::condition_variable notEmpty_;
std::condition_variable notFull_;
std::queue<_Tp> queue_;
size_t count_{ 0 };
size_t bound_;
};
}
#endif
//在visual studio 2013上编译运行通过
// file : main.cpp
#include <cstdio>
#include <cstdlib>
#include <thread>
#include "BlockBoundQueue.h"
yang::BlockBoundQueue<int> blockboundqueue_;// 全局的缓冲边界队列
const int total = 16; // 商品总数
void consumer(size_t id,size_t n); // 消费者
void producer(size_t id,size_t n); // 生产者
int main()
{
std::thread consumer1(consumer,0, 5);
std::thread consumer2(consumer,1, 5);
std::thread consumer3(consumer,2, 6);
std::thread producer1(producer,0, 8);
std::thread producer2(producer,1, 8);
consumer1.join();
consumer2.join();
consumer3.join();
producer1.join();
producer2.join();
system("pause");
return 0;
}
void consumer(size_t id,size_t n)
{
for (auto i = 0; i < n; ++i)
{
std::this_thread::sleep_for(std::chrono::seconds(2));
int item = blockboundqueue_.get();
printf("the %d^th consumer thread has consumed the %d^th item\n", id,item);
}
} void producer(size_t id,size_t n)
{
for (int i = 0; i < n; ++i)
{
blockboundqueue_.push(i);
printf("the %d^th producer thread has produced the %d^th item\n",id, i);
}
}
【转】https://blog.csdn.net/dhz625/article/details/72026666
用BlockBoundQueue和c++11实现多线程生产者消费者问题的更多相关文章
- java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】
java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-[费元星Q9715234] 说明如下,不懂的问题直接我[费元星Q9715234] 1.反射的意义在于不将xml tag ...
- Python多线程-生产者消费者模型
用多线程和队列来实现生产者消费者模型 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import threading imp ...
- Java实现多线程生产者消费者模式的两种方法
生产者消费者模式:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据.生产者生产一个,消费者消费一个,不断循环. 第一种实现方法,用BlockingQueue阻塞队 ...
- 多线程-生产者消费者(synchronized同步)
正解博客:https://blog.csdn.net/u011863767/article/details/59731447 永远在循环(loop)里调用 wait 和 notify,不是在 If 语 ...
- java多线程 生产者消费者模式
package de.bvb; /** * 生产者消费者模式 * 通过 wait() 和 notify() 通信方法实现 * */ public class Test1 { public static ...
- [多线程] 生产者消费者模型的BOOST实现
说明 如果 使用过程中有BUG 一定要告诉我:在下面留言或者给我邮件(sawpara at 126 dot com) 使用boost::thread库来实现生产者消费者模型中的缓冲区! 仓库内最多可以 ...
- Java实现多线程生产者消费者模型及优化方案
生产者-消费者模型是进程间通信的重要内容之一.其原理十分简单,但自己用语言实现往往会出现很多的问题,下面我们用一系列代码来展现在编码中容易出现的问题以及最优解决方案. /* 单生产者.单消费者生产烤鸭 ...
- 操作系统实验 windows编程多线程 生产者消费者问题 画圆画方(内置bug版)
实验3:随便写的 #include <windows.h> #include <string> #include <stdio.h> #pragma warning ...
- java实现多线程生产者消费者模式
1.概念 生产者消费者模式就是通过一个容器来解决生产者和消费者的强耦合问题.生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消 ...
随机推荐
- Hyperledger Fabric 1.4 快速环境搭建
自己的硕士研究方向和区块链有关,工程上一直以IBM的Hyperledger Fabric为基础进行开发,对该项目关注也有两年了.目前迎来了Hyperledger Fabric v1.4,这也是Fabr ...
- Elasticsearch运维经验总结
Elasticsearch运维经验总结 2018年12月10日 16:38:41 运小白 阅读数 3811 版本说明:5.6.4(要严格注意ES及其插件.第三方工具的版本匹配关系) 系统负载:(日 ...
- cad.net 读取pc3,pmp 读取pc3打印机文件
修改pc3文件还没做好..大家先look look怎么读.... 首先弄一个控制台程序, 然后去下载 Ionic.Zip 这个东西...载到控制台...都很简单... 然后就是复制下面代码,看控制台显 ...
- N x N 的矩阵,顺时针旋转
第一种方法: 先打印外圈,再打印内圈 public class RotateMatrix1 { public static void rotate(int[][] matrix) { ; ; ; ]. ...
- 移动开发首页业界资讯移动应用平台技术专题 输入您要搜索的内容 基于Java Socket的自定义协议,实现Android与服务器的长连接(二)
在阅读本文前需要对socket以及自定义协议有一个基本的了解,可以先查看上一篇文章<基于Java Socket的自定义协议,实现Android与服务器的长连接(一)>学习相关的基础知识点. ...
- 在Eclipse中使用Beyond Compare做为比较工具
1.下载org.eclipse.externaltools-Update-0.8.9.v201003051612.zip插件包 接下来,要下载Beyond Compare的插件,http://beyo ...
- (8)ASP.NET Core 中的MVC路由一
1.前言 ASP.NET Core MVC使用路由中间件来匹配传入请求的URL并将它们映射到操作(Action方法).路由在启动代码(Startup.Configure方法)或属性(Controlle ...
- K8S学习笔记之K8S日志搜集实战
详细参考这篇文章,几乎覆盖了了K8S的各种日志搜集方案 https://juejin.im/post/5b6eaef96fb9a04fa25a0d37#heading-8
- k-匿名算法
30 November 2019 18:31 人类历史上,除了计算机外从没有一项技术可以在短短的几十年间,能够全方位的影响整个社会的各个领域.技术的发展,少不了许多代人为之的努力.无论是在计算 ...
- php调用webservice接口,java代码接收不到参数
前段时间做了一个项目的二次开发,有个功能是需要php调用java实现的webservice接口,并传递一些参数给接口,然后按照对方提供的接口说明文档进行传参调用,java那边有接收到请求,但是参数总是 ...