代码拉取完成,页面将自动刷新
#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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。