#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;
}