ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
PosixPrioritySharedQueue< T, _Priority > Class Template Reference

Thread-safe FIFO priority queue class. More...

#include <PosixPrioritySharedQueue.hpp>

Public Member Functions

 PosixPrioritySharedQueue ()
 Constructor. Initialize the queue.
 ~PosixPrioritySharedQueue ()
 Destructor. Clean up the resources.
void addQueue (const _Priority &prio)
 Adds new priority.
void push (const T &data, const _Priority &prio)
 Insert an new element in the queue.
pop ()
 Extract an element from the queue.
void clear ()
 Empties the queue.
size_t size () const
 The current size of the queue.

Private Member Functions

 PosixPrioritySharedQueue (const PosixPrioritySharedQueue &)
PosixPrioritySharedQueueoperator= (const PosixPrioritySharedQueue &)

Private Attributes

std::map< _Priority,
std::queue< T > > 
queues_
pthread_cond_t empty_
pthread_mutex_t mutex_
size_t globalSize_

Detailed Description

template<typename T, typename _Priority = int>
class onposix::PosixPrioritySharedQueue< T, _Priority >

Thread-safe FIFO priority queue class.

No aging techniques are implemented, so that the low priority elements can starve. The class is noncopyable.
The template paramaters are:

  • T is the type of the element in the queue
  • _Priority is the priority type (shall support the less operator)

Definition at line 24 of file PosixPrioritySharedQueue.hpp.

Constructor & Destructor Documentation

PosixPrioritySharedQueue ( const PosixPrioritySharedQueue< T, _Priority > &  )
private

Constructor. Initialize the queue.

Exceptions
runtime_errorif the initialization fails.

Definition at line 56 of file PosixPrioritySharedQueue.hpp.

:
{
if (pthread_mutex_init(&mutex_, NULL) != 0)
throw std::runtime_error(std::string("Mutex initialization: ") +
strerror(errno));
if (pthread_cond_init(&empty_, NULL) != 0)
throw std::runtime_error(
std::string("Condition variable initialization: ") +
strerror(errno));
}

Destructor. Clean up the resources.

Definition at line 72 of file PosixPrioritySharedQueue.hpp.

{
VERIFY_ASSERTION(!pthread_mutex_destroy(&mutex_));
VERIFY_ASSERTION(!pthread_cond_destroy(&empty_));
}

Member Function Documentation

void addQueue ( const _Priority &  prio)

Adds new priority.

If the priority already exists does nothing.

Parameters
prioThe priority to be added.

Definition at line 85 of file PosixPrioritySharedQueue.hpp.

{
PthreadMutexLocker lock(mutex_);
if (queues_.find(prio) == queues_.end())
queues_.insert(std::make_pair<_Priority, std::queue<T> >(
prio, std::queue<T>()));
}
void clear ( )

Empties the queue.

In order to efficiently accomplish its task, this function exchanges its content with an empty queue using the specialized version of swap() implemented for the STL container std::map.

Definition at line 151 of file PosixPrioritySharedQueue.hpp.

{
PthreadMutexLocker lock(mutex_);
std::map<_Priority, std::queue<T> > empty;
std::swap(queues_, empty);
}
PosixPrioritySharedQueue& operator= ( const PosixPrioritySharedQueue< T, _Priority > &  )
private
T pop ( )

Extract an element from the queue.

If the queue is empty the calling thread is blocked.

Returns
The first of the highest priority element in the queue.

Definition at line 121 of file PosixPrioritySharedQueue.hpp.

{
PthreadMutexLocker lock(mutex_);
while (!globalSize_)
pthread_cond_wait(&empty_, &mutex_);
typename std::map<_Priority, std::queue<T> >::iterator it = queues_.begin();
typename std::map<_Priority, std::queue<T> >::iterator itEnd =
queues_.end();
for(; it != itEnd; ++it){
if (!(it->second).empty()){
T data = (it->second).front();
(it->second).pop();
return data;
}
}
assert(false);
//Just to silent the compiler
T data;
return data;
}
void push ( const T &  data,
const _Priority &  prio 
)

Insert an new element in the queue.

Parameters
dataThe element to be added.
prioThe priority of the element.

Definition at line 100 of file PosixPrioritySharedQueue.hpp.

{
pthread_mutex_lock(&mutex_);
if (queues_.find(prio) != queues_.end()){
queues_[prio].push(data);
pthread_mutex_unlock(&mutex_);
pthread_cond_signal(&empty_);
return;
}
pthread_mutex_unlock(&mutex_);
}
size_t size ( ) const

The current size of the queue.

Returns
The queue size.

Definition at line 165 of file PosixPrioritySharedQueue.hpp.

{
PthreadMutexLocker lock(mutex_);
return globalSize_;
}

Member Data Documentation

pthread_cond_t empty_
private

Definition at line 27 of file PosixPrioritySharedQueue.hpp.

size_t globalSize_
private

Definition at line 29 of file PosixPrioritySharedQueue.hpp.

pthread_mutex_t mutex_
mutableprivate

Definition at line 28 of file PosixPrioritySharedQueue.hpp.

std::map< _Priority, std::queue<T> > queues_
private

Definition at line 26 of file PosixPrioritySharedQueue.hpp.


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