Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

OW32::CCircularBuffer< T > Class Template Reference

An N producers/N consumers circular buffer implementation. More...

#include <circularbuffer.h>

List of all members.

Public Member Functions

 CCircularBuffer (size_t queueSize)
 Default constructor which builds an empty buffer.
 ~CCircularBuffer ()
 D'tor that cleans up the allocated buffer space.
bool isBroken ()
 Test if broken (for timeouts/polling and use of Break).
void Break ()
 Cause any threads blocked on the getElement and putElement functions to return immediately.
void ResetBreak ()
 Reset the shutdown event; subsequent calls to getElement or putElement may block if the buffer is empty or full respectively.
bool putElement (const T &el, DWORD nTimeout=INFINITE)
 Enter a data item into the buffer.
bool getElement (T &el, DWORD nTimeout=INFINITE)
 Consume a data item from the buffer.


Detailed Description

template<class T>
class OW32::CCircularBuffer< T >

An N producers/N consumers circular buffer implementation.


Constructor & Destructor Documentation

template<class T>
OW32::CCircularBuffer< T >::CCircularBuffer size_t  queueSize  )  [inline]
 

Default constructor which builds an empty buffer.

Note: the space for the items is allocated here, in advance, on the heap. Circular buffers are generally used for communication between high priority and low priority threads (for example, for a log). In this case, we don't want the high priority thread to block on anything, including memory allocation; hence the advance allocation.

Thus, if you are storing large objects in the buffer, consider storing pointers to them to save the copies/excessive memory consumption that may otherwise be caused.


Member Function Documentation

template<class T>
void OW32::CCircularBuffer< T >::Break  )  [inline]
 

Cause any threads blocked on the getElement and putElement functions to return immediately.

This is generally useful when you are shutting down an application and your worker threads are blocked on getElement() or putElement(). These functions will return false indicating that no data was placed into the buffer or consumed from it.

template<class T>
bool OW32::CCircularBuffer< T >::getElement T &  el,
DWORD  nTimeout = INFINITE
[inline]
 

Consume a data item from the buffer.

Parameters:
el The data item consumed.
nTimeout How long to wait for items to be entered into the buffer. Defaults to forever.
Returns:
true if the item was dequeued successfully, false if not.
This function can return false under one of two conditions:
  • A timeout was encountered (iff nTimeout != INFINITE)
  • The break event has been set (generally during shutdown, see Break()).

template<class T>
bool OW32::CCircularBuffer< T >::putElement const T &  el,
DWORD  nTimeout = INFINITE
[inline]
 

Enter a data item into the buffer.

Parameters:
el The data item to enter.
nTimeout How long to wait for space in the buffer. Defaults to forever.
Returns:
true if the item was queued successfully, false if not.
This function can return false under one of two conditions:
  • A timeout was encountered (iff nTimeout != INFINITE)
  • The break event has been set (generally during shutdown, see Break()).

template<class T>
void OW32::CCircularBuffer< T >::ResetBreak  )  [inline]
 

Reset the shutdown event; subsequent calls to getElement or putElement may block if the buffer is empty or full respectively.

This can be used after an aborted shutdown.


The documentation for this class was generated from the following file:
Generated on Sun Jun 5 01:29:23 2005 for OW32 by  doxygen 1.3.9.1