2 Star 6 Fork 3

qazxlf / NetSurveyW

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
my_ringbuffbased_fifo.h 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
#ifndef MY_RINGBUFFBASEDFIFO_H
#define MY_RINGBUFFBASEDFIFO_H
#include "my_fifo.h"
template<typename T>
class my_ringbuffbased_fifo :
public my_fifo<T>
{
public:
my_ringbuffbased_fifo(unsigned long size=10000);
public:
~my_ringbuffbased_fifo(void);
virtual int init();
virtual int push_back(T content);
virtual int pop_front(T& content);
int disabled();
int enabled();
virtual int clear();
private:
T* base;
typedef enum
{
ENABLED,
DISABLED
}STATUS;
STATUS _status;
};
template<typename T>
my_ringbuffbased_fifo<T>::my_ringbuffbased_fifo(unsigned long size):my_fifo<T>(size),base(0)
{
}
template<typename T>
my_ringbuffbased_fifo<T>::~my_ringbuffbased_fifo(void)
{
ACE_Guard<ACE_Recursive_Thread_Mutex> guard(this->mutex);
if (base)
{
delete [] base;
}
}
template<typename T>
int my_ringbuffbased_fifo<T>::init()
{
ACE_Guard<ACE_Recursive_Thread_Mutex> guard(this->mutex);
if (!base)
{
base=new T[this->maxSize];
if (base)
{
this->full=0;
return 0;
}
}
return -1;
}
template<typename T>
int my_ringbuffbased_fifo<T>::push_back(T content) //插入 最多等30秒 否则说明处理太慢
{
if (!base)
{
return -1;
}
ACE_Time_Value time_out_v=ACE_Time_Value(30)+ACE_OS::gettimeofday();
int ret=0;
{
//my_ace_guard guard(this->mutex);
ACE_Guard<ACE_Recursive_Thread_Mutex> guard(this->mutex);
while (this->is_full()==0)
{
ret=this->condNotfull.wait(&time_out_v);
if (ret==-1)
{
return 2;
}
if (this->_status==this->DISABLED)
{
return 1;
}
}
if (this->_status==this->DISABLED)
{
return 1;
}
base[this->tail]=content;
this->tail=(this->tail+1)%this->maxSize;
if (this->head==this->tail)
{
this->full=1;
}
this->condNotempty.signal();
}
return 0;
}
template<typename T>
int my_ringbuffbased_fifo<T>::pop_front(T& content) //弹出 可以永久等待
{
if (!base)
{
return -1;
}
//ACE_Time_Value time_out_v=ACE_Time_Value(30)+ACE_OS::gettimeofday();
int ret=0;
{
//my_ace_guard guard(this->mutex);
ACE_Guard<ACE_Recursive_Thread_Mutex> guard(this->mutex);
while (this->is_empty()==0)
{
ret=this->condNotempty.wait(/*&time_out_v*/);
if (ret==-1)
{
return 1;
}
if (this->_status==this->DISABLED)
{
return 1;
}
}
if (this->_status==this->DISABLED)
{
return 1;
}
content=base[this->head];
this->head=(this->head+1)%this->maxSize;
if (this->head==this->tail)
{
this->full=0;
}
this->condNotfull.signal();
}
return 0;
}
template<typename T>
int my_ringbuffbased_fifo<T>::disabled()
{
ACE_Guard<ACE_Recursive_Thread_Mutex> guard(this->mutex);
this->_status=this->DISABLED;
this->condNotempty.broadcast();
this->condNotfull.broadcast();
return 0;
}
template<typename T>
int my_ringbuffbased_fifo<T>::enabled()
{
ACE_Guard<ACE_Recursive_Thread_Mutex> guard(this->mutex);
this->_status=this->ENABLED;
return 0;
}
template<typename T>
int my_ringbuffbased_fifo<T>::clear(){
ACE_Guard<ACE_Recursive_Thread_Mutex> guard(this->mutex);
this->head=0;
this->tail=0;
this->full=0;
return 0;
}
#endif
C++
1
https://gitee.com/xulongfei0612/NetSurveyW.git
git@gitee.com:xulongfei0612/NetSurveyW.git
xulongfei0612
NetSurveyW
NetSurveyW
master

搜索帮助