zoukankan      html  css  js  c++  java
  • 双缓存静态循环队列(二)

     1 // CritcalS.h: interface for the CCritcalS class.
     2 //
     3 //////////////////////////////////////////////////////////////////////
     4 
     5 #if !defined(AFX_CRITCALS_H__4875D14E_4C88_4E3C_AF59_4F085B8FFBBA__INCLUDED_)
     6 #define AFX_CRITCALS_H__4875D14E_4C88_4E3C_AF59_4F085B8FFBBA__INCLUDED_
     7 
     8 #if _MSC_VER > 1000
     9 #pragma once
    10 #endif // _MSC_VER > 1000
    11 
    12 
    13 class CCritcalS  
    14 {
    15 public:
    16     void Free();
    17     void Lock();
    18     CCritcalS();
    19     virtual ~CCritcalS();
    20 
    21 private:    
    22     CRITICAL_SECTION m_critcal;
    23     BOOL m_IsLock;
    24 };
    25 
    26 #endif // !defined(AFX_CRITCALS_H__4875D14E_4C88_4E3C_AF59_4F085B8FFBBA__INCLUDED_)
    CCritcalS_H
     1 // CritcalS.cpp: implementation of the CCritcalS class.
     2 //
     3 //////////////////////////////////////////////////////////////////////
     4 
     5 #include "stdafx.h"
     6 #include "OverlapRoutine.h"
     7 #include "CritcalS.h"
     8 
     9 #ifdef _DEBUG
    10 #undef THIS_FILE
    11 static char THIS_FILE[]=__FILE__;
    12 #define new DEBUG_NEW
    13 #endif
    14 
    15 //////////////////////////////////////////////////////////////////////
    16 // Construction/Destruction
    17 //////////////////////////////////////////////////////////////////////
    18 
    19 CCritcalS::CCritcalS()
    20 {
    21     InitializeCriticalSection(&this->m_critcal);
    22     m_IsLock = FALSE;
    23 }
    24 
    25 CCritcalS::~CCritcalS()
    26 {
    27 
    28 }
    29 
    30 void CCritcalS::Lock()
    31 {
    32     EnterCriticalSection(&this->m_critcal);
    33     if(this->m_IsLock)
    34     {
    35         ASSERT(0);
    36     }
    37 
    38     this->m_IsLock = TRUE;
    39 }
    40 
    41 void CCritcalS::Free()
    42 {
    43     this->m_IsLock = FALSE;
    44     LeaveCriticalSection(&this->m_critcal);
    45 }
    CCritcalS_CPP
     1 // BufQueue.h: interface for the CBufQueue class.
     2 //
     3 //////////////////////////////////////////////////////////////////////
     4 
     5 #if !defined(AFX_BUFQUEUE_H__667975DA_4709_4F1C_A6F5_F07587C86E0E__INCLUDED_)
     6 #define AFX_BUFQUEUE_H__667975DA_4709_4F1C_A6F5_F07587C86E0E__INCLUDED_
     7 
     8 #if _MSC_VER > 1000
     9 #pragma once
    10 #endif // _MSC_VER > 1000
    11 
    12 #include "CritcalS.h"
    13 
    14 // 静态FIFO循环队列,服务器端作为读写缓存
    15 #define MAX_CMP(x,y) x>=y?x:y
    16 #define MIN_CMP(x,y) x<=y?x:y
    17 const QUEUESIZE = 1024*32;   //队列大小
    18 
    19 class CBufQueue  
    20 {
    21 public:
    22     int Read(char *pBuf, int readBytes);
    23     int GetfreeSize();
    24     int GetdataSize();
    25     int Write(const char *pBuf, int writeBytes);
    26     CBufQueue();
    27     virtual ~CBufQueue();
    28 private:
    29     void next(int &index);
    30     char pop();
    31     void push(char data);
    32 
    33     char m_charArray[QUEUESIZE];
    34     int m_indexH;  // 对列头  第一个数据位的索引值
    35     int m_indexT;  // 队列尾  第一个空闲位的索引值
    36 
    37     CCritcalS m_critcal;
    38 };
    39 
    40 #endif // !defined(AFX_BUFQUEUE_H__667975DA_4709_4F1C_A6F5_F07587C86E0E__INCLUDED_)
    41 
    42 // 静态循环队算法
    43 /*
    44 
    45 1)永远保留一个空闲位置: 只有队列为空时:m_indexH等于m_indexTT;
    46                         满队列的容量为: QUEUESIZE-1
    47 
    48 2)初始空队列状态: m_indexH = 0
    49                   m_indexT = 0
    50 
    51 3)CUR为当前索引位置,则NEXT索引位计算公式: NEXT = (CUR+1)% QUEUESIZE
    52 
    53 4)队列数据容量SIZE计算: if(m_indexT>=m_indexH)
    54                            SIZE = m_indexT - m_indexH
    55 
    56                         if(m_indexT<m_indexH)
    57                            SIZE = m_indexT - m_indexH + QUEUESIZE
    58 
    59 5)在插入数据时,当数据容量等于QUEUESIZE-1时判断队列满 (禁止插入)
    60 
    61 */
    CBufQueue _H
     1 // BufQueue.cpp: implementation of the CBufQueue class.
     2 //
     3 //////////////////////////////////////////////////////////////////////
     4 
     5 #include "stdafx.h"
     6 #include "OverlapRoutine.h"
     7 #include "BufQueue.h"
     8 
     9 #ifdef _DEBUG
    10 #undef THIS_FILE
    11 static char THIS_FILE[]=__FILE__;
    12 #define new DEBUG_NEW
    13 #endif
    14 
    15 //////////////////////////////////////////////////////////////////////
    16 // Construction/Destruction
    17 //////////////////////////////////////////////////////////////////////
    18 
    19 CBufQueue::CBufQueue()
    20 {
    21     this->m_indexH = 0;
    22     this->m_indexT = 0;
    23 }
    24 
    25 CBufQueue::~CBufQueue()
    26 {
    27 
    28 }
    29 
    30 int CBufQueue::Write(const char *pBuf, int writeBytes)
    31 {    
    32     m_critcal.Lock();
    33      
    34     int sum = this->GetfreeSize();
    35     int    tSize = MIN_CMP(writeBytes,sum);
    36     for(int i=0;i<tSize;i++)
    37     {
    38         this->push(pBuf[i]);
    39     }
    40     m_critcal.Free();
    41 
    42     return tSize;
    43 }
    44 
    45 int CBufQueue::Read(char *pBuf, int readBytes)
    46 {
    47     m_critcal.Lock();
    48     
    49     int sum = this->GetdataSize();
    50     int tSize = MIN_CMP(readBytes,sum);
    51     for(int i=0;i<tSize;i++)
    52     {
    53         pBuf[i] = this->pop();
    54     }
    55     m_critcal.Free();
    56 
    57     return tSize;
    58 
    59 }
    60 
    61 // 队尾入队,由外层函数做队满判断
    62 void CBufQueue::push(char data)
    63 {
    64     this->m_charArray[this->m_indexT] = data;  // 
    65     next(this->m_indexT);
    66 }
    67 
    68 // 对头出队,有外层函数作队空判断
    69 char CBufQueue::pop()
    70 {
    71     char res =     this->m_charArray[this->m_indexH];
    72     next(this->m_indexH);
    73 
    74     return res;
    75 }
    76 
    77 // 获得队列数据容量
    78 int CBufQueue::GetdataSize()
    79 {
    80     if(m_indexT>=m_indexH)
    81     {
    82         return (m_indexT - m_indexH);
    83     }else
    84     {
    85         return (m_indexT - m_indexH + QUEUESIZE);
    86     }
    87 }
    88 
    89 // 获得队列空闲容量
    90 int CBufQueue::GetfreeSize()
    91 {
    92     return (QUEUESIZE-this->GetdataSize() - 1);
    93 }
    94 
    95 // 索引下滑计算
    96 void CBufQueue::next(int &index)
    97 {
    98     index = (index+1)% QUEUESIZE;
    99 }
    CBufQueue_CPP
  • 相关阅读:
    Oracle statspack 安装及使用
    MyEclipse 快捷键
    ORACLE EXP/IMP的使用详解
    PyCharm如何配置断点调试功能
    ftl-server静态资源服务器
    JS计算两个日期时间差,天 小时 分 秒格式
    readystatechange
    js实现input中输入数字,控制每四位加一个空格(银行卡号格式)
    javascript“类”与继承总结和回顾
    Javascript根据指定下标或对象删除数组元素
  • 原文地址:https://www.cnblogs.com/Esperanto/p/5353982.html
Copyright © 2011-2022 走看看