1 // QueueT.h: interface for the CQueueT class. 2 // 3 // 4 5 #if !defined(AFX_QUEUET_H__BB664F37_5738_4439_8220_A54436AE6339__INCLUDED_) 6 #define AFX_QUEUET_H__BB664F37_5738_4439_8220_A54436AE6339__INCLUDED_ 7 8 #include "CritcalS.h" 9 10 #if _MSC_VER > 1000 11 #pragma once 12 #endif // _MSC_VER > 1000 13 14 // 循环队列: 模板类 15 16 // 要求类型 TYPE 支持默认构造函数、赋值构造函数 17 #define MAX_CMP(x,y) x>=y?x:y 18 #define MIN_CMP(x,y) x<=y?x:y 19 const int QUEUET_SIZE = 1024; //队列大小 20 template21 class CQueueT 22 { 23 public: 24 CQueueT(); 25 virtual ~CQueueT(); 26 27 int Read(TYPE *pBuf, int readBytes); 28 int GetfreeSize(); 29 int GetdataSize(); 30 int Write(const TYPE *pBuf, int writeBytes); 31 private: 32 void next(int &index); 33 TYPE pop(); 34 void push(TYPE data); 35 36 TYPE m_charArray[QUEUET_SIZE]; 37 int m_indexH; // 对列头 第一个数据位的索引值 38 int m_indexT; // 队列尾 第一个空闲位的索引值 39 40 CCritcalS m_critcal; // 临界段 41 }; 42 43 / 44 template 45 CQueueT ::CQueueT() 46 { 47 48 } 49 50 template 51 CQueueT ::~CQueueT() 52 { 53 54 } 55 56 template 57 int CQueueT ::Write(const TYPE *pBuf, int writeBytes) 58 { 59 m_critcal.Lock(); 60 61 int sum = this->GetfreeSize(); 62 int tSize = MIN_CMP(writeBytes,sum); 63 for(int i=0;i push(pBuf[i]); 66 } 67 68 m_critcal.Free(); 69 70 return tSize; 71 } 72 73 template 74 int CQueueT ::Read(TYPE *pBuf, int readBytes) 75 { 76 m_critcal.Lock(); 77 78 int sum = this->GetdataSize(); 79 int tSize = MIN_CMP(readBytes,sum); 80 for(int i=0;i pop(); 83 } 84 m_critcal.Free(); 85 86 return tSize; 87 } 88 89 // 队尾入队,由外层函数做队满判断 90 template 91 void CQueueT ::push(TYPE data) 92 { 93 this->m_charArray[this->m_indexT] = data; // 94 next(this->m_indexT); 95 } 96 97 // 对头出队,有外层函数作队空判断 98 template 99 TYPE CQueueT ::pop()100 {101 TYPE res = this->m_charArray[this->m_indexH];102 next(this->m_indexH);103 104 return res;105 }106 107 // 获得队列数据容量108 template 109 int CQueueT ::GetdataSize()110 {111 if(m_indexT>=m_indexH)112 {113 return (m_indexT - m_indexH);114 }else115 {116 return (m_indexT - m_indexH + QUEUET_SIZE);117 }118 }119 120 // 获得队列空闲容量121 template 122 int CQueueT ::GetfreeSize()123 {124 return (QUEUESIZE-this->GetdataSize() - 1);125 }126 127 // 索引下滑计算128 template 129 void CQueueT ::next(int &index)130 {131 index = (index+1)% QUEUET_SIZE;132 }133 134 #endif // !defined(AFX_QUEUET_H__BB664F37_5738_4439_8220_A54436AE6339__INCLUDED_)
1 // TwoBufQueue.h: interface for the CTwoBufQueue class. 2 // 3 // 4 5 #if !defined(AFX_TWOBUFQUEUE_H__EA19608F_9562_4803_95C4_5C4A574CC928__INCLUDED_) 6 #define AFX_TWOBUFQUEUE_H__EA19608F_9562_4803_95C4_5C4A574CC928__INCLUDED_ 7 8 #if _MSC_VER > 1000 9 #pragma once 10 #endif // _MSC_VER > 1000 11 #include "QueueT.h" 12 13 // 双缓存队列,减少读写操作的互斥碰撞,向无锁算法靠近,乒乓存储原理 14 15 /* 16 1)对于读写双方读写操作你比较频繁,双方的的瞬时吞吐量存在差异,可以很好的利用缓存空间特性提读写效率。 17 18 2)消费数据的速度总是大于生产数据的的速度,本数据结构与单缓存队列无异。 19 20 3)可以减少了读写互斥消耗,同时读,同时写互斥仍存在。 21 */ 22 23 template24 class CTwoBufQueue 25 { 26 public: 27 unsigned int GetdataSize(); 28 unsigned int Write(const TYPE *pBuf, unsigned int sum); 29 unsigned int Read(TYPE *pBuf, unsigned int sum); 30 CTwoBufQueue(); 31 virtual ~CTwoBufQueue(); 32 private: 33 void private_SwitchPointer(); 34 CQueueT m_Queue_A; 35 CQueueT m_Queue_B; 36 CQueueT *m_pRead_Q; 37 CQueueT *m_pWrite_Q; 38 CCritcalS m_critcal; 39 }; 40 //// 41 template 42 CTwoBufQueue ::CTwoBufQueue() 43 { 44 this->m_pRead_Q = &m_Queue_A; 45 this->m_pWrite_Q = &m_Queue_B; 46 } 47 template 48 CTwoBufQueue ::~CTwoBufQueue() 49 { 50 51 } 52 53 // 交换缓存对象指针 54 template 55 void CTwoBufQueue ::private_SwitchPointer() 56 { 57 CBufQueue *pTemp = this->m_pRead_Q; 58 this->m_pRead_Q = this->m_pWrite_Q; 59 this->m_pWrite_Q = pTemp; 60 } 61 62 63 template 64 unsigned int CTwoBufQueue ::Read(TYPE *pBuf, unsigned int readBytes) 65 { 66 int res = this->m_pRead_Q->Read(pBuf,readBytes); 67 if(res>0) 68 { 69 return res; 70 }else //读缓存空 ,交换缓存对象指针 71 { 72 this->m_critcal.Lock(); 73 this->private_SwitchPointer(); 74 this->m_critcal.Free(); 75 76 return this->m_pRead_Q->Read(pBuf,readBytes); 77 } 78 79 } 80 81 82 // 写操作指针对当前buf对象,不做交换 83 template 84 unsigned int CTwoBufQueue ::Write(const TYPE *pBuf, unsigned int sum) 85 { 86 this->m_critcal.Lock(); 87 int res = this->m_pWrite_Q->Write(pBuf,sum); 88 this->m_critcal.Free(); 89 90 return res; 91 92 } 93 94 95 template 96 unsigned int CTwoBufQueue ::GetdataSize() 97 { 98 return this->m_pRead_Q->GetdataSize()+this->m_pWrite_Q->GetdataSize(); 99 }100 101 #endif // !defined(AFX_TWOBUFQUEUE_H__EA19608F_9562_4803_95C4_5C4A574CC928__INCLUDED_)