#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <string>
#include <time.h>
#define MAX_SIZE 100
using namespace std;
//生产完成标准
bool isDone = false;
//基础休息时间
const int pwait = 100;
const int cwait = 200;
//生产总数,生产者数量,消费者数量,缓冲区最大值
int limit, producerNum, consumeNum, maxsize;
//当前生产数量
int currentLimit = 0;
//生产数量统计数组
int producerCount[MAX_SIZE] = { 0 };
//生产ID统计数组
int producerID[MAX_SIZE] = { 0 };
//生产数量统计数组
int consumeCount[MAX_SIZE] = { 0 };
//生产ID统计数组
int consumeID[MAX_SIZE] = { 0 };
//互斥信号量,缓冲区满信号量,缓冲区空信号量,打印信号量
HANDLE mutex, full, empty,disp;
//生产者函数
DWORD WINAPI Producer(LPVOID param)
{
int id; //记录生产者编号
//遍历获取生产者编号
for (int i = 0; i < producerNum; i++)
{
if (producerID[i] == GetCurrentThreadId())
id = i;
}
//输出互斥
WaitForSingleObject(disp, INFINITE);
cout << "生产者"<< id <&l
C++之多个生产者与多个消费者的多线程问题
最新推荐文章于 2024-04-21 21:55:21 发布