生产者-消费者问题(多缓冲区版)Windows环境下C++实现

#include<iostream>#include<queue>#include<Windows.h>#include<unordered_map>using namespace std;static const int PRODUCER_SIZE = 2;//生产者数量static const int CONSUMER_SIZE ...
摘要由CSDN通过智能技术生成
#include<iostream>
#include<queue>
#include<Windows.h>
#include<unordered_map>

using namespace std;

static const int PRODUCER_SIZE = 2;//生产者数量
static const int CONSUMER_SIZE = 5;//消费者数量
static const int QUEUE_SIZE = 3;//单个缓冲区大小
static const int POOL_SIZE = 3;//缓冲区个数

static HANDLE event = NULL;//定时退出事件
static queue<int> bufferQueuePool[POOL_SIZE];//缓冲池
static CRITICAL_SECTION queueMutex[POOL_SIZE];//每个缓冲区的读写锁
static CRITICAL_SECTION coutMutex;//输出锁,为了避免输出混乱
static HANDLE semEmpty[POOL_SIZE];//每个缓冲区的空位信号量
static HANDLE semFull[POOL_SIZE];//每个缓冲区的满信号量
static int goodsNum = 0;//当前生产商品的序号
unordered_map<DWORD, int> id;//hashmap用来记录线程id与进程序号的映射

DWORD WINAPI ProducerThread()
{
   
	while (true)
	{
   
		if (WAIT_TIMEOUT != WaitForSingleObject(event, 0))
			break;

		DWORD emptyPoolId = WaitForMultipleObjects(POOL_SIZE
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值