#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
生产者-消费者问题(多缓冲区版)Windows环境下C++实现
最新推荐文章于 2023-03-21 09:33:52 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)