RSC  0.17.1
rsc::threading::SynchronizedQueue< M > Class Template Reference

A queue with synchronized access and interruption support. More...

#include <SynchronizedQueue.h>

Inheritance diagram for rsc::threading::SynchronizedQueue< M >:
Inheritance graph
Collaboration diagram for rsc::threading::SynchronizedQueue< M >:
Collaboration graph

Public Types

typedef boost::function< void(const M &drop)> dropHandlerType
 

Public Member Functions

 SynchronizedQueue (const unsigned int &sizeLimit=0, dropHandlerType dropHandler=0)
 Creates a new queue. More...
 
virtual ~SynchronizedQueue ()
 
void push (const M &message)
 Pushes a new element on the queue and wakes up one waiting client if there is one. More...
 
peek (const boost::uint32_t &timeoutMs=0)
 Returns the element at the front of the queue without removing it, waiting until there is such an element if necessary. More...
 
pop (const boost::uint32_t &timeoutMs=0)
 Returns the next element form the queue and wait until there is such an element. More...
 
tryPop ()
 Tries to pop an element from the queue but does not wait if there is no element on the queue. More...
 
bool empty () const
 Checks whether this queue is empty. More...
 
std::size_t size () const
 Return element count of the queue. More...
 
void clear ()
 Remove all elements from the queue. More...
 
void interrupt ()
 Interrupts the processing on this queue for every current call to pop and any later call. More...
 

Private Attributes

volatile bool interrupted
 
std::queue< M > queue
 
boost::recursive_mutex mutex
 
boost::condition condition
 
unsigned int sizeLimit
 
dropHandlerType dropHandler
 

Detailed Description

template<class M>
class rsc::threading::SynchronizedQueue< M >

A queue with synchronized access and interruption support.

On push operations only one waiting thread is woken up.

Author
jwienke
Template Parameters
Mmessage type handled in this queue

Definition at line 62 of file SynchronizedQueue.h.

Member Typedef Documentation

template<class M>
typedef boost::function<void(const M& drop)> rsc::threading::SynchronizedQueue< M >::dropHandlerType

Definition at line 65 of file SynchronizedQueue.h.

Constructor & Destructor Documentation

template<class M>
rsc::threading::SynchronizedQueue< M >::SynchronizedQueue ( const unsigned int &  sizeLimit = 0,
dropHandlerType  dropHandler = 0 
)
inlineexplicit

Creates a new queue.

Parameters
sizeLimitmax allowed size of the queue. If the limit is reached and another element is added to the queue, the oldest element is removed. 0 means unlimited

Definition at line 86 of file SynchronizedQueue.h.

template<class M>
virtual rsc::threading::SynchronizedQueue< M >::~SynchronizedQueue ( )
inlinevirtual

Definition at line 91 of file SynchronizedQueue.h.

Member Function Documentation

template<class M>
void rsc::threading::SynchronizedQueue< M >::clear ( )
inline

Remove all elements from the queue.

Definition at line 250 of file SynchronizedQueue.h.

template<class M>
bool rsc::threading::SynchronizedQueue< M >::empty ( ) const
inline

Checks whether this queue is empty.

Is not affected by the interruption flag.

Returns
true if empty, else false

Definition at line 232 of file SynchronizedQueue.h.

template<class M>
void rsc::threading::SynchronizedQueue< M >::interrupt ( )
inline

Interrupts the processing on this queue for every current call to pop and any later call.

All of them will return with an exception.

Definition at line 262 of file SynchronizedQueue.h.

template<class M>
M rsc::threading::SynchronizedQueue< M >::peek ( const boost::uint32_t &  timeoutMs = 0)
inline

Returns the element at the front of the queue without removing it, waiting until there is such an element if necessary.

Parameters
timeoutMsmaximum time spent waiting for a new element in milliseconds. Zero indicates to wait endlessly.
Returns
front element of the queue
Exceptions
InterruptedExceptionif interrupt was called before a call to this method or while waiting for an element
QueueEmptyExceptionthrown if
Parameters
timeoutMswas > 0 and no element was found on the queue in the specified time

Definition at line 134 of file SynchronizedQueue.h.

template<class M>
M rsc::threading::SynchronizedQueue< M >::pop ( const boost::uint32_t &  timeoutMs = 0)
inline

Returns the next element form the queue and wait until there is such an element.

The returned element is removed from the queue immediately.

Parameters
timeoutMsmaximum time spent waiting for a new element in milliseconds. Zero indicates to wait endlessly. Use tryPop if you want to get a result without waiting
Returns
next element on the queue
Exceptions
InterruptedExceptionif interrupt was called before a call to this method or while waiting for an element
QueueEmptyExceptionthrown if
Parameters
timeoutMswas > 0 and no element was found on the queue in the specified time

Definition at line 175 of file SynchronizedQueue.h.

template<class M>
void rsc::threading::SynchronizedQueue< M >::push ( const M &  message)
inline

Pushes a new element on the queue and wakes up one waiting client if there is one.

Parameters
messageelement to push on the queue

Definition at line 104 of file SynchronizedQueue.h.

template<class M>
std::size_t rsc::threading::SynchronizedQueue< M >::size ( ) const
inline

Return element count of the queue.

Returns
The number of elements currently stored in the queue.

Definition at line 242 of file SynchronizedQueue.h.

template<class M>
M rsc::threading::SynchronizedQueue< M >::tryPop ( )
inline

Tries to pop an element from the queue but does not wait if there is no element on the queue.

Returns
element from the queue
Exceptions
QueueEmptyExceptionthe queue was empty

Definition at line 212 of file SynchronizedQueue.h.

Member Data Documentation

template<class M>
boost::condition rsc::threading::SynchronizedQueue< M >::condition
private

Definition at line 72 of file SynchronizedQueue.h.

template<class M>
dropHandlerType rsc::threading::SynchronizedQueue< M >::dropHandler
private

Definition at line 74 of file SynchronizedQueue.h.

template<class M>
volatile bool rsc::threading::SynchronizedQueue< M >::interrupted
private

Definition at line 69 of file SynchronizedQueue.h.

template<class M>
boost::recursive_mutex rsc::threading::SynchronizedQueue< M >::mutex
mutableprivate

Definition at line 71 of file SynchronizedQueue.h.

template<class M>
std::queue<M> rsc::threading::SynchronizedQueue< M >::queue
private

Definition at line 70 of file SynchronizedQueue.h.

template<class M>
unsigned int rsc::threading::SynchronizedQueue< M >::sizeLimit
private

Definition at line 73 of file SynchronizedQueue.h.


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