首页 > 其他分享 >生产者消费者问题

生产者消费者问题

时间:2023-04-27 14:35:57浏览次数:27  
标签:std index 消费者 生产者 问题 int num include con

1、

// condition_variable::notify_one
#include <iostream>           // std::cout
#include <thread>             // std::thread
#include <mutex>              // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
using namespace std; 
std::mutex mtx;
std::condition_variable produce,consume;
 
int cargo = 0;     // shared value by producers and consumers
 
void consumer () {
  std::unique_lock<std::mutex> lck(mtx);
  cout << "before consumer wait " << endl;
  while (cargo==0) consume.wait(lck);
  std::cout << cargo << '\n';
  cargo=0;
  cout << "consumer cargo : " << cargo << endl;
  cout << "this is consumer " << endl;
  produce.notify_one();
}
 
void producer (int id) {
  std::unique_lock<std::mutex> lck(mtx);
  cout << "before producer wait " << endl;
  while (cargo!=0) produce.wait(lck);
  cargo = id;
  cout << "producer cargo : " << cargo << endl;
  cout << "this is producer " << endl;
  consume.notify_one();
}
 
int main ()
{
  std::thread consumers[10],producers[10];
  // spawn 10 consumers and 10 producers:
  for (int i=0; i<10; ++i) {
    consumers[i] = std::thread(consumer);
    producers[i] = std::thread(producer,i+1);
  }
 
  // join them back:
  for (int i=0; i<10; ++i) {
    producers[i].join();
    consumers[i].join();
  }
 
  return 0;
}

 2、

 

//生产者-消费者问题3
#include<iostream>
#include<mutex>
#include<thread>
#include<string>
#include <condition_variable>
 
using namespace std;
 
//预处理
#define MAX 10//缓冲池的最大数量
 
//全局变量的定义
mutex m;//建立一个互斥信号量m
condition_variable full_con;//如果某时刻缓冲池为满,那么就让full_con等待
condition_variable empty_con;//如果某时刻缓冲池为空,那么就让empty等待
string buffer[MAX];//建立一个最大数量为MAX的缓冲池,此缓冲池采用循环队列的形式
int pro_num = 0;//用pro_num来记录生产者在缓冲池的当前位置
int con_num = 0;//用con_num来记录消费者在缓冲池的当前位置
int pro_index = 0;//用index来记录现在是第几个被生产者生产的产品
 
//生产者类
class producer{
private:
	string in;//生产的产品
	int index;//用来标记现在是第几个生产者
	
public:
	//带默认形参的构造函数
	producer( string in = "", int index = 0 ) : in( in ), index( index ){
	} 
	
	//调用系统默认的析构函数
	~producer( ) = default;
	
	//生产产品,用time来决定循环几次
	void produce( );
};
 
class consumer{
private:
	string out;//消费的产品
	int index;//用来标记现在是第几个消费者
	
public:
	//带默认形参的构造函数
	consumer( string out = "", int index = 0 ) : out( out ), index( index ){
	}
	
	//调用系统默认的析构函数
	~consumer( ) = default;
	
	//消费产品
	void consume( );
};
 
//生产产品函数的实现
void producer::produce( ){
		in = "生产者“" + to_string( this->index ) + "”生产的产品" + to_string( pro_index++ );//生产的产品
		unique_lock lock( m );//锁住互斥变量m,避免消费者动用缓冲池buffer
		while(( pro_num + 1 ) % MAX == con_num ){//判断缓冲池已满
			cout << "【!!!】缓冲池已满,生产者“" << index << "”在等待一个空位" << endl;
			full_con.wait( lock );//让full进入等待状态
		}
		buffer[pro_num] = in;//将生产者生产的产品放入缓冲池
		pro_num = ( pro_num +1 ) % MAX;
		cout << in << endl;	
		empty_con.notify_all();//唤起empty	
}
 
//消费产品函数的实现
void consumer::consume(){
		unique_lock lock( m );//锁住互斥变量m,避免生产者动用缓冲池buffer
		while( pro_num == con_num ){//判断缓冲池为空
			cout << "【!!!】缓冲池为空,消费者“" << index << "”在等待一件产品" << endl;
			empty_con.wait( lock );
		}
		out = "消费者“" + to_string( this->index ) + "”消费了" + buffer[con_num];//消费者消费的产品
		con_num = ( con_num + 1 ) % MAX;//将生产者生产的产品从缓冲池取出
		cout << out << endl;	
		full_con.notify_all();//唤起full
}
 
//定义了一个函数,让生产者生产50次产品
void producer_work( producer p ) {
	int i = 0;
	while( i < 50 ){
		p.produce( );
		i++;
	}
}
 
//定义了一个函数,让消费者消费50次产品	
void consumer_work( consumer c ){
	int i = 0;
	while( i < 50 ){
		c.consume( );
		i++;
	}
}
  
int main( void ){
	producer p;
	consumer c;
	thread pro( producer_work, p );
	thread con( consumer_work, c);
	pro.join( );
	con.join( );
	return 0;
}

 

标签:std,index,消费者,生产者,问题,int,num,include,con
From: https://www.cnblogs.com/xiaochouk/p/17358791.html

相关文章

  • Grid/RAC 11.2.0.4 与 Linux 7 的一些兼容性问题
    1、在LINUX6上安装11.2.0.4的RAC,基本上不会遇到什么问题,但如果在LINUX7上安装11.2.0.4的RAC,经常性地会遇到问题。为了很好地解决这个问题,ORACLE官方在MOS上给了一篇文档《Installationwalk-through-OracleGrid/RAC11.2.0.4onOracleLinux7(DocID1951613.1)》,这篇文档......
  • 使用ethtool排查网卡速率问题
    今天去现场帮一个客户排查备份网络速率问题。用户期望是万兆的速率,但实际上目前只有千兆,因为目前上面运行着数据库,且数据量较大,千兆的备份网络速率不能满足用户备份数据库的时长要求。首先,确认备份网络是由两块网卡(eth3,eth4)做了bonding,起名为bondeth1。使用ethtool查看底层的et......
  • 【解决方法】正常游览Flash页面,解决主流游览器的不支持问题(如Edge,Firefox)
    环境:工具:Firefox84版本或者360游览器-某特殊版本系统版本:Windows10问题描述:描述:进入某需要Flash插件的管理网站,使用edge等最新主流游览器均无法调用Flash,开启IE模式也没有做用,网上的其他教程也因为时效性已经无法生效。问题解释:解释:各大主流游览器在2020年基本都......
  • 记一次线上服务器问题排查过程
    问题描述前几天我们更新线上服务器,使用对应的新版客户端连接时,怎么都连不上,如果直接连接其他服,比如我们内部的测试服或者审核服,却一切正常。同时,如果使用老包连接服务器,也是正常的这个问题查的头疼,每一步都超出我的理解范围排查过程首先第一步,我们在服务器接口的必经之路上......
  • 记一次线上服务器问题排查过程
    问题描述前几天我们更新线上服务器,使用对应的新版客户端连接时,怎么都连不上,如果直接连接其他服,比如我们内部的测试服或者审核服,却一切正常。同时,如果使用老包连接服务器,也是正常的这个问题查的头疼,每一步都超出我的理解范围排查过程首先第一步,我们在服务器接口的必经之路上......
  • 记一次线上服务器问题排查过程
    问题描述前几天我们更新线上服务器,使用对应的新版客户端连接时,怎么都连不上,如果直接连接其他服,比如我们内部的测试服或者审核服,却一切正常。同时,如果使用老包连接服务器,也是正常的这个问题查的头疼,每一步都超出我的理解范围排查过程首先第一步,我们在服务器接口的必经之路上......
  • 记一次线上服务器问题排查过程
    问题描述前几天我们更新线上服务器,使用对应的新版客户端连接时,怎么都连不上,如果直接连接其他服,比如我们内部的测试服或者审核服,却一切正常。同时,如果使用老包连接服务器,也是正常的这个问题查的头疼,每一步都超出我的理解范围排查过程首先第一步,我们在服务器接口的必经之路上......
  • golang1.6版本json包解析嵌套指针的问题小记
    指针的指针问题本地跑的好好的,测试环境跑的好好,预发布环境(准线上环境),跪了。起因就是:1a:=&struct{s:""}2json.Unmarshal([]byte{},&a)3fmt.Println(a.s)//报错行第一行代码进行&取地址,获得指针变量。第二行代码,进行json解析的时候,传入了&a, 指针的指针,a到了jso......
  • map注入 ,使用 @ConfigurationProperties , 修改apollo配置后不会动态修改的问题
    问题:apollo动态刷新,应用在@value这种注入方式的属性没有问题,但是如果使用@ConfigurationProperties注解的bean,动态刷新就不好使了,会注入不到的。Apollo动态刷新官网介绍需要注意的是,@ConfigurationProperties如果需要在Apollo配置变化时自动更新注入的值,需要配合使用Environm......
  • NET CORE 跨域问题
    在StartUp类里面配置如下代码即可解决跨域问题publicvoidConfigureServices(IServiceCollectionservices){services.AddCors(p=>p.AddPolicy("corsapp",builder=>{builder.WithOrigins("*").AllowAnyMethod().Allo......