c++多线程生产者与消费者问题代码

本文介绍了一种使用C++实现的多线程解决方案,关注于生产者和消费者问题。通过BufferPool.h头文件展示了相关代码实现。
摘要由CSDN通过智能技术生成

1.目录结构

2.代码

BufferPool.h

#ifndef BUFFERPOOL
#define BUFFERPOOL
#include<windows.h>
#include<vector>
#include<iostream>
#include<string>
#define SIZE_OF_BUFFER 4
using namespace std;
//控制参数类
class BufferPool
{
public:
	static BufferPool * GetInstance();
	
	void Produce();
	void Consume();
	void Product_Sum();

	//多线程互斥信号量
	HANDLE mutex;
	HANDLE mutex2;

	//多线程同步信号量
	//空缓冲区
	HANDLE m_hSemaphoreBufferEmpty;
	//非空缓冲区
	HANDLE m_hSemaphoreBufferFull;

	unsigned int produce_sum;            //the total produce number
	unsigned int consume_sum;            //the total consume number

private:

	unsigned short in;                   //the mark of position entering the space 
	unsigned short out;                  //the mark of position leaving the space
	unsigned int Product_ID;
	unsigned int Consume_ID;

	static BufferPool *m_Instance;
	int buffer[SIZE_OF_BUFFER];          //缓冲池

	BufferPool()
	{
		int i = 0;
		in = out = 0;
		produce_sum = consume_sum = 0;
		Product_ID = Consume_ID = 0;
		for(;i < SIZE_OF_BUFFER; i++)
			buffer[i] = 0;
	
	};

};
#endif
Consume.h

#ifndef CONSUME
#define CONSUME
#include "BufferPool.h"
#include<windows.h>
#include<vector>
#include<iostream>
#include<string>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值