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

Implementation of a thread safe FIFO queue class. More...

#include <PosixSharedQueue.hpp>

Public Member Functions

 PosixSharedQueue ()
 Constructor. Initialize the queue.
 ~PosixSharedQueue ()
 Destructor. Clean up the resources.
void push (const T &data)
 Inserts an element in the queue.
pop ()
 Extracts an element from the queue.
void clear ()
 Empties the queue.
size_t size () const
 The current size of the queue.

Private Member Functions

 PosixSharedQueue (const PosixSharedQueue &)
PosixSharedQueueoperator= (const PosixSharedQueue &)

Private Attributes

std::queue< T > queue_
pthread_cond_t empty_
pthread_mutex_t mutex_

Detailed Description

template<typename T>
class onposix::PosixSharedQueue< T >

Implementation of a thread safe FIFO queue class.

The template parameter is the type of the elements contained in the queue. The class is non copyable and makes use of POSIX threads (pthreads).

Definition at line 41 of file PosixSharedQueue.hpp.

Constructor & Destructor Documentation

PosixSharedQueue ( const PosixSharedQueue< T > &  )
private

Constructor. Initialize the queue.

Exceptions
runtime_errorif the initialization fails.

Definition at line 69 of file PosixSharedQueue.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 83 of file PosixSharedQueue.hpp.

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

Member Function Documentation

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::queue.

Definition at line 131 of file PosixSharedQueue.hpp.

{
PthreadMutexLocker lock(mutex_);
std::queue<T> empty;
std::swap(queue_, empty);
}
PosixSharedQueue& operator= ( const PosixSharedQueue< T > &  )
private
T pop ( )

Extracts an element from the queue.

Blocks the calling thread if the queue is empty.

Returns
The first element in the queue.

Definition at line 111 of file PosixSharedQueue.hpp.

{
PthreadMutexLocker lock(mutex_);
while (queue_.empty())
if (pthread_cond_wait(&empty_, &mutex_) != 0)
throw std::runtime_error(std::string("Condition variable wait: ") +
strerror(errno));
T data = queue_.front();
queue_.pop();
return data;
}
void push ( const T &  data)

Inserts an element in the queue.

Parameters
dataThe element to be added in the queue.

Definition at line 95 of file PosixSharedQueue.hpp.

{
{
PthreadMutexLocker lock(mutex_);
queue_.push(data);
}
pthread_cond_signal(&empty_);
}
size_t size ( ) const

The current size of the queue.

Returns
The queue size.

Definition at line 143 of file PosixSharedQueue.hpp.

{
PthreadMutexLocker lock(mutex_);
return queue_.size();
}

Member Data Documentation

pthread_cond_t empty_
private

Definition at line 44 of file PosixSharedQueue.hpp.

pthread_mutex_t mutex_
mutableprivate

Definition at line 45 of file PosixSharedQueue.hpp.

std::queue<T> queue_
private

Definition at line 43 of file PosixSharedQueue.hpp.


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