My Project
Public Types | Public Member Functions | Public Attributes | List of all members
ParaEngine::CParaRingBuffer< T > Class Template Reference

CParaRingBuffer is normally used in a producer-consumer mode when producer threads produce items and store them in the container and consumer threads remove these items and process them. More...

#include <ParaRingBuffer.h>

Public Types

enum  BufferStatus {
  BufferOverFlow = 0, BufferFull = 1, BufferNormal = 2, BufferEmpty = 3,
  BufferFirst = 3
}
 
typedef boost::circular_buffer< T > container_type
 
typedef container_type::size_type size_type
 
typedef container_type::value_type value_type
 

Public Member Functions

 CParaRingBuffer (size_type capacity)
 
BufferStatus try_push (const value_type &item)
 try push to back of the queue. More...
 
BufferStatus try_push_get_front (const value_type &item, value_type **ppFrontItem)
 same as try_push, except that it also returns pointer to the front object. More...
 
void push_front (const value_type &item)
 push to the front of the queue. More...
 
BufferStatus try_push_array (const value_type *pItems, int nCount)
 try push to back of the queue. More...
 
value_type * try_front ()
 
bool try_pop (value_type &item)
 try pop from the front of the queue. More...
 
bool try_next (value_type **ppItem)
 try pop from the front of the queue and return the front object after the pop. More...
 
size_type size () const
 
bool empty () const
 
bool full () const
 

Public Attributes

container_type m_container
 
mutex m_mutex
 

Detailed Description

template<class T>
class ParaEngine::CParaRingBuffer< T >

CParaRingBuffer is normally used in a producer-consumer mode when producer threads produce items and store them in the container and consumer threads remove these items and process them.

The bounded buffer has to guarantee that producers do not insert items into the container when the container is full, that consumers do not try to remove items when the container is empty, and that each produced item is consumed by exactly one consumer.

Note
: most functions are thread-safe, a light mutex is used in place.

Member Function Documentation

§ push_front()

template<class T>
void ParaEngine::CParaRingBuffer< T >::push_front ( const value_type &  item)
inline

push to the front of the queue.

this is usually for high priority messages. If queue is full, the last one will be removed.

Parameters
itemthe object to add
Note
: thread safe

§ try_front()

template<class T>
value_type* ParaEngine::CParaRingBuffer< T >::try_front ( )
inline
Returns
: get a pointer to the front object if exist, or NULL.
Note
this is thread safe, however the returned object may be invalid if it is pop when you use it.

§ try_next()

template<class T>
bool ParaEngine::CParaRingBuffer< T >::try_next ( value_type **  ppItem)
inline

try pop from the front of the queue and return the front object after the pop.

Parameters
ppValueFrontget a pointer to the front object after the operation.
Returns
: return true if succeed, false if queue is empty.
Note
: thread safe

§ try_pop()

template<class T>
bool ParaEngine::CParaRingBuffer< T >::try_pop ( value_type &  item)
inline

try pop from the front of the queue.

Parameters
ppValueFrontget a pointer to the front object after the operation.
Returns
: return true if succeed, false if queue is empty.
Note
: thread safe

§ try_push()

template<class T>
BufferStatus ParaEngine::CParaRingBuffer< T >::try_push ( const value_type &  item)
inline

try push to back of the queue.

Parameters
itemthe object to add
Returns
: return buffer status after the item is added. If BufferOverFlow, it means adding is failed; if BufferFirst, this is the first item added. if BufferFull, the buffer is full after inserting the new one, in other cases, BufferNormal is returned.
Note
: thread safe

§ try_push_array()

template<class T>
BufferStatus ParaEngine::CParaRingBuffer< T >::try_push_array ( const value_type *  pItems,
int  nCount 
)
inline

try push to back of the queue.

Returns
: return buffer status after the item is added. If BufferOverFlow, it means adding is failed; if BufferFirst, this is the first item added. if BufferFull, the buffer is full after inserting the new one, in other cases, BufferNormal is returned.
Note
: thread safe

§ try_push_get_front()

template<class T>
BufferStatus ParaEngine::CParaRingBuffer< T >::try_push_get_front ( const value_type &  item,
value_type **  ppFrontItem 
)
inline

same as try_push, except that it also returns pointer to the front object.

Parameters
ppFrontItemthere is no guarantee that the front object pointer is valid after return. please ensure no other thread is popping items when you are accessing the front item.

The documentation for this class was generated from the following file: