用生产者消费者模型实现的线程安全环形队列

#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H

#include <iostream>
#include <vector>
#include <windows.h>
#include "CurrentPrice.h"

//template<class T>
class CircularBuffer
{
public:
	CircularBuffer(int capacity);
	~CircularBuffer();
public:
	void push_back(const CurrentPrice& currentPrice);
	CurrentPrice pop_front();
	bool empty();
private:
	HANDLE m_mutexSemaphore;
	HANDLE m_fullSemaphore, m_emptySemaphore;

	int m_begin, m_end, m_size;
	int m_capacity;
	std::vector<CurrentPrice> m_currentPrices;
};

class CircularBufferStock
{
public:
	CircularBufferStock(int capacity);
	~CircularBufferStock();
public:
	void push_back(const STOCK_PRICE& currentPrice);
	STOCK_PRICE pop_front();
	bool empty();
private:
	HANDLE m_mutexSemaphore;
	HANDLE m_fullSemaphore, m_emptySemaphore;

	int m_begin, m_end, m_size;
	int m_capacity;
	std::vector<STOCK_PRICE> m_currentPrices;
};
#endif
#include "stdafx.h"
#include "CircularBuffer.h"

//template<class T>
CircularBuffer::CircularBuffer(int capacity)
	:  m_begin(0), m_end(0), m_size(0), m_capacity(capacity), m_currentPrices(capacity)
{
	m_emptySemaphore=CreateSemaphore(NULL,capacity,capacity,NULL);//创建信号量empty_Semaphore
	m_fullSemaphore=CreateSemaphore(NULL,0,capacity,NULL);//创建信号量full_Semaphore
	m_mutexSemaphore=CreateSemaphore(NULL,1,1,NULL);//创建互斥信号量mutex_Semaphore
}

//template<class T>
CircularBuffer::~CircularBuffer(){}

//template<class T>
void CircularBuffer::push_back(const CurrentPrice& currentPrice)
{
	WaitForSingleObject(m_emptySemaphore,-1);
	WaitForSingleObject(m_mutexSemaphore,-1);

	m_currentPrices[m_end] = currentPrice;
	m_end = (m_end+1) % m_capacity;
	++m_size;

	ReleaseSemaphore(m_mutexSemaphore, 1, NULL);
	ReleaseSemaphore(m_fullSemaphore,1,NULL);
}

//template<class T>
CurrentPrice CircularBuffer::pop_front()
{
	WaitForSingleObject(m_fullSemaphore,-1);
	WaitForSingleObject(m_mutexSemaphore,-1);

	//CurrentPrice currentPrice = m_currentPrices.front();
	CurrentPrice currentPrice = m_currentPrices[m_begin];
	m_begin = (m_begin+1) % m_capacity;
	--m_size;

	ReleaseSemaphore(m_mutexSemaphore, 1, NULL);
	ReleaseSemaphore(m_emptySemaphore,1,NULL);
	return currentPrice;
}

//template<class T>
bool CircularBuffer::empty()
{
	//return m_currentPrices.empty();
	return m_begin == m_end;
}

/
//class CricularBufferStock
CircularBufferStock::CircularBufferStock(int capacity)
: m_begin(0), m_end(0), m_size(0), m_capacity(capacity), m_currentPrices(capacity)
{
	m_emptySemaphore = CreateSemaphore(NULL, capacity, capacity, NULL);//创建信号量empty_Semaphore
	m_fullSemaphore = CreateSemaphore(NULL, 0, capacity, NULL);//创建信号量full_Semaphore
	m_mutexSemaphore = CreateSemaphore(NULL, 1, 1, NULL);//创建互斥信号量mutex_Semaphore
}

CircularBufferStock::~CircularBufferStock(){}

void CircularBufferStock::push_back(const STOCK_PRICE& currentPrice)
{
	WaitForSingleObject(m_emptySemaphore, -1);
	WaitForSingleObject(m_mutexSemaphore, -1);

	m_currentPrices[m_end] = currentPrice;
	m_end = (m_end + 1) % m_capacity;
	++m_size;

	ReleaseSemaphore(m_mutexSemaphore, 1, NULL);
	ReleaseSemaphore(m_fullSemaphore, 1, NULL);
}

STOCK_PRICE CircularBufferStock::pop_front()
{
	WaitForSingleObject(m_fullSemaphore, -1);
	WaitForSingleObject(m_mutexSemaphore, -1);

	//CurrentPrice currentPrice = m_currentPrices.front();
	STOCK_PRICE currentPrice = m_currentPrices[m_begin];
	m_begin = (m_begin + 1) % m_capacity;
	--m_size;

	ReleaseSemaphore(m_mutexSemaphore, 1, NULL);
	ReleaseSemaphore(m_emptySemaphore, 1, NULL);
	return currentPrice;
}

bool CircularBufferStock::empty()
{
	//return m_currentPrices.empty();
	return m_begin == m_end;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值