The Micro C++ Library
micxx::PCQueue< T > Class Template Reference

A PCQueue implements a threadsafe blocking Producer/Consumer Queue supporting a maximum size. More...

#include <micxx.hxx>

Inherits micxx::Condition.

List of all members.

Public Member Functions

 PCQueue (unsigned maxSize=16384)
 Create a new and empty PCQueue with the specified maxSize.
virtual ~PCQueue ()
virtual void clear ()
 Remove all elements from the queue and interrupt all currently blocking enqueue(T) or dequeue() calls.
virtual unsigned getMaxSize () const
 The maximum size of elements, that may be stored within this PCQueue.
virtual T & head ()
 A reference to the next element from this PCQueue, that would be returned by the next subsequent call of dequeue().
virtual T dequeue ()
 Remove and return the next element from this PCQueue.
virtual bool enqueue (T t)
 Add a copy of data element t to this PCQueue.
virtual std::size_t size ()
 The currently available elements within this PCQueue.

Protected Attributes

unsigned maxSize
std::list< T > queue
 The internal queue.

Detailed Description

template<typename T>
class micxx::PCQueue< T >

A PCQueue implements a threadsafe blocking Producer/Consumer Queue supporting a maximum size.

A PCQueue is typically used when there are one or more producer threads that continuously enqueue data elements into a queue and one or more consumer threads concurrently read continuously data elements from this queue.

Author:
Norbert Klose
Date:
December, 2006

Constructor & Destructor Documentation

template<typename T >
micxx::PCQueue< T >::PCQueue ( unsigned  maxSize = 16384)

Create a new and empty PCQueue with the specified maxSize.

Parameters:
maxSizeThe maximum size of this PCQueue. All enqueue(T) calls after reaching this size will block the current thread until a subsequent dequeue() removes one element from the queue.
                                                         :
    clearing(false),
    waiting(0),
    maxSize(maxSize)
{
}
template<typename T >
micxx::PCQueue< T >::~PCQueue ( ) [virtual]
{
}

Member Function Documentation

template<typename T >
void micxx::PCQueue< T >::clear ( ) [virtual]

Remove all elements from the queue and interrupt all currently blocking enqueue(T) or dequeue() calls.

{
    Synchronized synchronized(*this);
    queue.clear();
    clearing = true;
    notifyAll();
}
template<typename T >
T micxx::PCQueue< T >::dequeue ( ) [virtual]

Remove and return the next element from this PCQueue.

If there is cuurently no such element available, the calling thread is blocked until an new element is added by enqueue(T).

{
    Synchronized synchronized(*this);
    T t;
    while (queue.size() == 0 && !clearing)
    {
        ++waiting;
        wait();
        --waiting;
    }
    if (!clearing)
    {
        t = queue.front();
        queue.pop_front();
        notifyAll();
    }
    else if (!waiting)
    {
        clearing = false;
    }
    return t;
}
template<typename T >
bool micxx::PCQueue< T >::enqueue ( t) [virtual]

Add a copy of data element t to this PCQueue.

If the current size is greater or equal to this PCQueue's maximum size. All enqueue(T) calls after reaching this size will block the current thread until a subsequent dequeue() removes one element from the queue

{
    Synchronized synchronized(*this);
    bool result = false;
    while (queue.size() == maxSize && !clearing)
    {
        ++waiting;
        wait();
        --waiting;
    }
    if (!clearing)
    {
        queue.push_back(t);
        result = true;
        notifyAll();
    }
    else if (!waiting)
    {
        clearing = false;
    }
    return result;
}
template<typename T >
unsigned micxx::PCQueue< T >::getMaxSize ( ) const [inline, virtual]

The maximum size of elements, that may be stored within this PCQueue.

{
    return maxSize;
}
template<typename T >
T & micxx::PCQueue< T >::head ( ) [virtual]

A reference to the next element from this PCQueue, that would be returned by the next subsequent call of dequeue().

{
    Synchronized synchronized(*this);
    T & t = queue.front();
    return t;
}
template<typename T >
std::size_t micxx::PCQueue< T >::size ( ) [virtual]

The currently available elements within this PCQueue.

{
    Synchronized synchronized(*this);
    return queue.size();
}

Member Data Documentation

template<typename T >
unsigned micxx::PCQueue< T >::maxSize [protected]
template<typename T >
std::list<T> micxx::PCQueue< T >::queue [protected]

The internal queue.


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