Wefts::Queue< T > Class Template Reference
[Advanced Syncronization constructs]

#include <wefts_queue.h>

Collaboration diagram for Wefts::Queue< T >:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<class T>
class Wefts::Queue< T >

Waitable template FIFO structure.

A Queue, in Wefts++ context, is an object where one or more thread are posting data, and one or more thread are waiting for data to become available to process it.

Queue is a fast and clean way to pass arbitrary data in a 1:1 scheme from a producer to a consumer (or from a master to a worker) even if there are more producer/masters and consumer/workers running at the same time.

Queues have not any scheduling poicies; the first thread that is able to wake up will take the first element available.

By default, queues have infinite length. You can set a maximum length and in that case, a thread willing to post a new data will be blocked until the queue becomes free again, or will immediately return an error (depending on user preferences).

Note:
It is illegal to destroy a Queue while there are threads waiting. Class SmartQueue has a mechanism to prevent this problem, but this is somewhat more expensive in terms of speed.


Public Member Functions

 Queue (unsigned int maxLen=0)
 Creates a queue.

bool push (T elem, const bool wait=false)
 Pushes data in the queue.

bool pop (T &elem, const bool wait=true)
 Pops data from the queue.

bool empty () const
 Check wether a queue is empty or not.

int size () const

Protected Attributes

FastCondition m_cond
unsigned long m_maxLen
int m_size
std::list< T > m_list

Private Member Functions

void p_waitForFree ()
void p_waitForFull ()


Constructor & Destructor Documentation

template<class T>
Wefts::Queue< T >::Queue unsigned int  maxLen = 0  )  [inline]
 

Creates a queue.

If the parameter is zero, the queue will have infinite length; if it is not zero, push() method will block or return immediately false when the maximum length is reached.

Parameters:
maxLen maximum length of the queue or 0.


Member Function Documentation

template<class T>
bool Wefts::Queue< T >::empty  )  const [inline]
 

Check wether a queue is empty or not.

Notice that this operation has no sense if the queue is currently used in various threads. This is meant to be used by applications that are able to demonstrate that no thread is currently helding the queue, or by subclasses.

Returns:
true if the queue is empty.

template<class T>
void Wefts::Queue< T >::p_waitForFree  )  [inline, private]
 

template<class T>
void Wefts::Queue< T >::p_waitForFull  )  [inline, private]
 

template<class T>
bool Wefts::Queue< T >::pop T &  elem,
const bool  wait = true
[inline]
 

Pops data from the queue.

Queues are FIFO, that is First in, First out. The first element you push will be served first to the waiters.

Note that the default behavior of pop() is to wait until there is some data to be placed in the parameter, but you can also set wait parameter to false to have an immediate return in case of the queue being empty in that moment.

A blocking pop() is a cancellation point; be sure you have a proper cleanup scheme set up before to call a blocking pop.

Parameters:
elem an element that will receive the popped value.
wait if false, returns immediately if the queue is empty.
Returns:
true if operation suceeded, false if queue is full and wait parameter is false

template<class T>
bool Wefts::Queue< T >::push elem,
const bool  wait = false
[inline]
 

Pushes data in the queue.

Queues are FIFO, that is First in, First out. The first element you push will be served first to the waiters.

Parameters:
elem the data to be pushed in the Queue
wait true to wait in case the queue is full, false to return immediately
Returns:
true if operation suceeded, false if queue is full and wait parameter is false

template<class T>
int Wefts::Queue< T >::size  )  const [inline]
 


Member Data Documentation

template<class T>
FastCondition Wefts::Queue< T >::m_cond [protected]
 

template<class T>
std::list<T> Wefts::Queue< T >::m_list [protected]
 

template<class T>
unsigned long Wefts::Queue< T >::m_maxLen [protected]
 

template<class T>
int Wefts::Queue< T >::m_size [protected]
 


The documentation for this class was generated from the following file:
Generated on Tue Oct 5 14:57:03 2004 for Wefts by doxygen 1.3.7