C++11-->单生产者,单消费者问题

参考上一篇C++11并发编程

#include <iostream>
#include <queue>
#include <assert.h>
#include <condition_variable>
using namespace std;

/***经典单消费者,单生产者问题***/


/***货物类,简单的以一个int数据表示货物***/
class Item
{
public:
	Item(int i):data(i){}

	void display(){	cout << "Item: "<<data << endl;}
private:
	int data;
};

/***仓库类***/
/***m_CurrentPos	表示当前货物位置,-1表示空仓库***/
/***m_SumSize		表示可以容纳的货物数量***/
/***m_House			用来存放货物(存放Item*)***/
class StoreHouse
{
public:
	/***仓库容纳的货物数量***/
	StoreHouse(int size):m_SumSize(size),m_CurrentPos(-1)
	{
		m_House = new Item *[size];
		memset(m_House, 0, size * sizeof(Item *));
	}

	~StoreHouse()
	{
		delete[] m_House;
		m_House = NULL;
	}

	/***仓库是不是满了***/
	bool isFull()
	{
		return m_CurrentPos == m_SumSize - 1;
	}

	/***仓库是不是空的***/
	bool isEmpty()
	{
		return m_CurrentPos == -1;
	}

	/***朝仓库put进一个货物***/
	void put(Item *item)
	{
		assert((m_CurrentPos +1) != m_SumSize);
		++m_CurrentPos;
		m_House[m_CurrentPos] = item;
	}

	/***从仓库get出一个货物***/
	Item * get()
	{
		assert(m_CurrentPos != -1);
		Item *item = m_House[m_CurrentPos];
		m_House[m_CurrentPos] = NULL;
		--m_CurrentPos;
		return item;
	}

private:
	int			m_SumSize;
	int			m_CurrentPos;
	Item **		m_House;
};

StoreHouse gStoreHouse(4);/***仓库有4个货物空位***/
std::mutex mtx;
std::condition_variable cv_full;  /***仓库满的cv***/
std::condition_variable cv_empty;  /***仓库空的cv***/

int toProduceItem = 20;/***总共要生产的货物数量***/
int hasGet = 0;


/***生产一个货物***/
Item* produceItem(int itemdata)
{
	Item * item = new Item(itemdata);
	return item;
}

/***卖掉一个货物***/
void soldItem(Item *item)
{
	assert(item != NULL);
	
	item->display();
	delete item;
	item = NULL;
}

/***生产者task***/
void producerTask()
{
	for (int i = 0; i < toProduceItem; i++)
	{
		std::unique_lock<std::mutex>lck(mtx);
		while (gStoreHouse.isFull())/***如果仓库满,则通知当前生产者线程等待***/
		{
			cv_full.wait(lck);
		}

		Item *item = produceItem(i);/***造个货***/

		gStoreHouse.put(item);/***put一个货物进仓库后,现在仓库处于非空仓状态(有货物)***/

		cv_empty.notify_one();/***通知某个等待cv_empty的thread,告诉他现在是非空仓状态了(仓库有货,可以来get货了)***/
	}
	
	cout << "producerTask " << "id: " << " exit" << endl;
}

/***消费者task***/
void consummerTask()
{
	bool exited = false;
	while (true)
	{
		if (hasGet == toProduceItem)
		{
			exited = true;
		}
		else
		{
			std::unique_lock<std::mutex>lck(mtx);

			while (gStoreHouse.isEmpty())/***如果仓库空,则通知当前消费者线程等待***/
			{
				cv_empty.wait(lck);
			}

			Item *item = gStoreHouse.get();/***get一个货物出来,现在仓库不再满仓***/

			++hasGet;

			soldItem(item);/***卖掉货物***/

			cv_full.notify_one();/***通知某个等待cv_full的thread,告诉他现在不是满仓状态了(仓库没满,可以造货put到仓库了)***/
		}
	

		if (exited)
		{
			break;
		}
	}

	cout << "consummerTask "<<"id: "<<" exit" << endl;
}

int main()
{
	std::thread producer(producerTask);
	std::thread consummer0(consummerTask);
	
	producer.join();
	consummer0.join();
	

	system("pause");
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值