用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.概念 生产者消费者模式就是通过一个容器来解决生产者和消费者的强耦合问题.生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消 ...
随机推荐
- CentOS 7.6 安装Python3.7.2 多版本共存
CentOS 7.6 默认安装了 Python 2.7.5 准备环境 yum install git gcc gcc-c++ make automake autoconf libtool pcre p ...
- AKKA Inbox收件箱
起因 得到ActorRef就可以给actor发送消息,但无法接收多回复,也不知道actor是否停止 Inbox收件箱出现就是解决这两个问题 示例 package akka.demo.actor imp ...
- 第十二节:Asp.Net Core 之分布式缓存(SQLServer和Redis)
一. 整体说明 1. 说明 分布式缓存通常是指在多个应用程序服务器的架构下,作为他们共享的外部服务共享缓存,常用的有SQLServer.Redis.NCache. 特别说明一下:这里的分布式是 ...
- docker 安装 sqlserver 数据库
具备条件: 1.服务器需要大于2G内存.如果不够则可能无法正常启动,查看日志报如下错误:This program requires a machine with at least 2000 megab ...
- DotnetSpider爬虫简单示例 net core
文章地址 https://blog.csdn.net/sD7O95O/article/details/78097556 安装爬虫框架 NUGET 安装DotnetSpider 创建HTTP协议数据包 ...
- JavaScript由来
在互联网时代,网速还很差劲的时候,表单输入数据的合法性验证需要与服务器交换数据,从而加重了使用者的负担. 网景公司为了解决这种简单问题开发了JavaScript.在1995年2月网景公司在发布自己的浏 ...
- 1.0EnterpriseFrameWork 框架学习
1.先报其主页 :博主的框架是开源的 http://www.cnblogs.com/kakake/p/3938262.html . 2.学习的精髓是:该框架支持 ORM.SQL语句 和 存储过程 ,O ...
- C# vb .NET读取识别条形码线性条码code128
code128是比较常见的条形码编码规则类型的一种.如何在C#,vb等.NET平台语言里实现快速准确读取该类型条形码呢?答案是使用SharpBarcode! SharpBarcode是C#快速高效.准 ...
- ansible模块command、shell、raw、script
简介 环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v ...
- 前端开发常用 JS 方法
1,获取文件本地url,在上传之前预览 /** * 获取图片嗯滴url,在上传之前预览 * @param file 选择的图片文件 * @returns {*} url */ getFileLocat ...