续:双缓存队列_模板类

  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 template<class TYPE>
 21 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<class TYPE>
 45 CQueueT<TYPE>::CQueueT()
 46 {
 47 
 48 }
 49 
 50 template<class TYPE>
 51 CQueueT<TYPE>::~CQueueT()
 52 {
 53 
 54 }
 55 
 56 template<class TYPE>
 57 int CQueueT<TYPE>::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<tSize;i++)
 64     {
 65         this->push(pBuf[i]);
 66     }
 67 
 68     m_critcal.Free();
 69 
 70     return tSize;
 71 }
 72 
 73 template<class TYPE>
 74 int CQueueT<TYPE>::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<tSize;i++)
 81     {
 82         pBuf[i] = this->pop();
 83     }
 84     m_critcal.Free();
 85 
 86     return tSize;
 87 }
 88 
 89 // 队尾入队,由外层函数做队满判断
 90 template<class TYPE>
 91 void CQueueT<TYPE>::push(TYPE data)
 92 {
 93     this->m_charArray[this->m_indexT] = data;  // 
 94     next(this->m_indexT);
 95 }
 96 
 97 // 对头出队,有外层函数作队空判断
 98 template<class TYPE>
 99 TYPE CQueueT<TYPE>::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<class TYPE>
109 int CQueueT<TYPE>::GetdataSize()
110 {
111     if(m_indexT>=m_indexH)
112     {
113         return (m_indexT - m_indexH);
114     }else
115     {
116         return (m_indexT - m_indexH + QUEUET_SIZE);
117     }
118 }
119 
120 // 获得队列空闲容量
121 template<class TYPE>
122 int CQueueT<TYPE>::GetfreeSize()
123 {
124     return (QUEUESIZE-this->GetdataSize() - 1);
125 }
126 
127 // 索引下滑计算
128 template<class TYPE>
129 void CQueueT<TYPE>::next(int &index)
130 {
131     index = (index+1)% QUEUET_SIZE;
132 }
133 
134 #endif // !defined(AFX_QUEUET_H__BB664F37_5738_4439_8220_A54436AE6339__INCLUDED_)
CQueueT
  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 template<class TYPE>
 24 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<TYPE> m_Queue_A;
 35     CQueueT<TYPE> m_Queue_B;
 36     CQueueT<TYPE> *m_pRead_Q;
 37     CQueueT<TYPE> *m_pWrite_Q;
 38     CCritcalS m_critcal;
 39 };
 40 ////////////////////////////////////////////////////////////////
 41 template<class TYPE>
 42 CTwoBufQueue<TYPE>::CTwoBufQueue()
 43 {
 44      this->m_pRead_Q = &m_Queue_A;
 45      this->m_pWrite_Q = &m_Queue_B;
 46 }
 47 template<class TYPE>
 48 CTwoBufQueue<TYPE>::~CTwoBufQueue()
 49 {
 50 
 51 }
 52 
 53 // 交换缓存对象指针
 54 template<class TYPE>
 55 void CTwoBufQueue<TYPE>::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<class TYPE>
 64 unsigned int CTwoBufQueue<TYPE>::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<class TYPE>
 84 unsigned int CTwoBufQueue<TYPE>::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<class TYPE>
 96 unsigned int CTwoBufQueue<TYPE>::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_)
CTwoBufQueue

 

posted on 2016-04-07 11:51  雁北  阅读(286)  评论(0编辑  收藏  举报

导航