环形队列在只有一个入队线程和一个出队线程的情况下,是不需要使用锁的。此处的锁无关是只此种情况下,一个线程调用EnQueue,另一个线程调用DeQueue,不会发生冲突。
文件:include/Queue.h
文件:src/Queue.hpp
template < typename DataType >
Queue < DataType > ::Queue( int QueueSize):
QUEUE_SIZE(QueueSize),
m_Head( 0 ),
m_Tail( 0 )
... {
assert(QueueSize>0);
m_Data = new DataType[QueueSize];
}
template < typename DataType >
Queue < DataType > :: ~ Queue()
... {
delete[] m_Data;
m_Data = NULL;
}
template < typename DataType >
bool Queue < DataType > ::EnQueue(DataType & In)
... {
int nNext = (m_Tail==QUEUE_SIZE-1)?0 : m_Tail+1;
if (nNext==m_Head) //队满
...{
return false;
}
m_Data[m_Tail] = In;
m_Tail = nNext;
return true;
}
template < typename DataType >
bool Queue < DataType > ::DeQueue(DataType * Out)
... {
if (m_Head==m_Tail) //队空 多线程中的极端情况:队列中只有一个元素,但是出队错误
...{
return false;
}
*Out = m_Data[m_Head];
m_Head = (m_Head==QUEUE_SIZE-1) ? 0 : m_Head+1;
return true;
}
template < typename DataType >
int Queue < DataType > ::Count()
... {
if (m_Head==m_Tail)
...{
return 0;
}
else if (m_Head>m_Tail)
...{
return QUEUE_SIZE - (m_Head-m_Tail);
}
else
...{
return m_Tail-m_Head;
}
}
template < typename DataType >
bool Queue < DataType > ::IsEmpty()
... {
return m_Head==m_Tail;
}
template < typename DataType >
bool Queue < DataType > ::IsFull()
... {
return m_Head==((m_Tail==QUEUE_SIZE-1)?0 : m_Tail+1);
}
template < typename DataType >
bool Queue < DataType > ::GetFront(DataType * Out)
... {
if (m_Head==m_Tail) //队空
...{
return false;
}
int nLoc = (m_Head==QUEUE_SIZE-1) ? 0 : m_Head+1;
*Out = m_Data[nLoc];
return true;
}
测试:test/TestQueue.cpp
文件:include/Queue.h
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include < assert.h >
namespace DataStructure
... {
template<typename DataType>
class Queue
...{
public:
Queue(int QueueSize);
~Queue();
bool EnQueue(DataType& In);
bool DeQueue(DataType* Out);
int Count();
bool IsEmpty();
bool IsFull();
bool GetFront(DataType* Out);
private:
Queue();
Queue(const Queue& rsh);
Queue& operator=(const Queue& rsh);
//-----------------------------------------------------------------------------
protected:
private:
const int QUEUE_SIZE;
DataType *m_Data;
int m_Head;
int m_Tail;
};
#include "../src/Queue.hpp"
} // end namespace
#endif
#define _QUEUE_H_
#include < assert.h >
namespace DataStructure
... {
template<typename DataType>
class Queue
...{
public:
Queue(int QueueSize);
~Queue();
bool EnQueue(DataType& In);
bool DeQueue(DataType* Out);
int Count();
bool IsEmpty();
bool IsFull();
bool GetFront(DataType* Out);
private:
Queue();
Queue(const Queue& rsh);
Queue& operator=(const Queue& rsh);
//-----------------------------------------------------------------------------
protected:
private:
const int QUEUE_SIZE;
DataType *m_Data;
int m_Head;
int m_Tail;
};
#include "../src/Queue.hpp"
} // end namespace
#endif
文件:src/Queue.hpp
template < typename DataType >
Queue < DataType > ::Queue( int QueueSize):
QUEUE_SIZE(QueueSize),
m_Head( 0 ),
m_Tail( 0 )
... {
assert(QueueSize>0);
m_Data = new DataType[QueueSize];
}
template < typename DataType >
Queue < DataType > :: ~ Queue()
... {
delete[] m_Data;
m_Data = NULL;
}
template < typename DataType >
bool Queue < DataType > ::EnQueue(DataType & In)
... {
int nNext = (m_Tail==QUEUE_SIZE-1)?0 : m_Tail+1;
if (nNext==m_Head) //队满
...{
return false;
}
m_Data[m_Tail] = In;
m_Tail = nNext;
return true;
}
template < typename DataType >
bool Queue < DataType > ::DeQueue(DataType * Out)
... {
if (m_Head==m_Tail) //队空 多线程中的极端情况:队列中只有一个元素,但是出队错误
...{
return false;
}
*Out = m_Data[m_Head];
m_Head = (m_Head==QUEUE_SIZE-1) ? 0 : m_Head+1;
return true;
}
template < typename DataType >
int Queue < DataType > ::Count()
... {
if (m_Head==m_Tail)
...{
return 0;
}
else if (m_Head>m_Tail)
...{
return QUEUE_SIZE - (m_Head-m_Tail);
}
else
...{
return m_Tail-m_Head;
}
}
template < typename DataType >
bool Queue < DataType > ::IsEmpty()
... {
return m_Head==m_Tail;
}
template < typename DataType >
bool Queue < DataType > ::IsFull()
... {
return m_Head==((m_Tail==QUEUE_SIZE-1)?0 : m_Tail+1);
}
template < typename DataType >
bool Queue < DataType > ::GetFront(DataType * Out)
... {
if (m_Head==m_Tail) //队空
...{
return false;
}
int nLoc = (m_Head==QUEUE_SIZE-1) ? 0 : m_Head+1;
*Out = m_Data[nLoc];
return true;
}
测试:test/TestQueue.cpp
#include
<
stdio.h
>
#include " ../include/Stack.h "
#include " ../include/Queue.h "
#include < pthread.h >
#include < windows.h >
using namespace DataStructure;
void * EnQueue( void * param) // 入队线程
... {
Queue<int>* q = (Queue<int> *)param;
int i = 0;
while (i<100)
...{
if (q->EnQueue(i))
...{
i++;
}
else
...{
//printf("队满! ");
::Sleep(0);
}
}
return NULL;
}
void * DeQueue( void * param) // 出队线程
... {
Queue<int>* q = (Queue<int> *)param;
int i;
int j = 0;
while (true)
...{
if (!q->DeQueue(&i))
...{
//printf("队空! ");
j++;
::Sleep(0);
}
else
...{
printf("%d ", i);
}
if (i==99)
...{
break;
}
}
return NULL;
}
void Test1()
... {
Queue<int> q(3);
pthread_t t1;
pthread_create(&t1, NULL, EnQueue, (void*)&q);
pthread_t t2;
pthread_create(&t2, NULL, DeQueue, (void*)&q);
//挂起
pthread_join(t1, NULL);
pthread_join(t2, NULL);
}
int main()
... {
Test1();
printf("=============================== ");
getchar();
return 1;
}
#include " ../include/Stack.h "
#include " ../include/Queue.h "
#include < pthread.h >
#include < windows.h >
using namespace DataStructure;
void * EnQueue( void * param) // 入队线程
... {
Queue<int>* q = (Queue<int> *)param;
int i = 0;
while (i<100)
...{
if (q->EnQueue(i))
...{
i++;
}
else
...{
//printf("队满! ");
::Sleep(0);
}
}
return NULL;
}
void * DeQueue( void * param) // 出队线程
... {
Queue<int>* q = (Queue<int> *)param;
int i;
int j = 0;
while (true)
...{
if (!q->DeQueue(&i))
...{
//printf("队空! ");
j++;
::Sleep(0);
}
else
...{
printf("%d ", i);
}
if (i==99)
...{
break;
}
}
return NULL;
}
void Test1()
... {
Queue<int> q(3);
pthread_t t1;
pthread_create(&t1, NULL, EnQueue, (void*)&q);
pthread_t t2;
pthread_create(&t2, NULL, DeQueue, (void*)&q);
//挂起
pthread_join(t1, NULL);
pthread_join(t2, NULL);
}
int main()
... {
Test1();
printf("=============================== ");
getchar();
return 1;
}