c++-gtk-utils
|
This file provides thread-safe asynchronous queue classes. More...
#include <queue>
#include <list>
#include <exception>
#include <algorithm>
#include <time.h>
#include <c++-gtk-utils/mutex.h>
#include <c++-gtk-utils/thread.h>
#include <c++-gtk-utils/cgu_config.h>
#include <unistd.h>
Go to the source code of this file.
Classes | |
class | Cgu::AsyncQueuePopError |
An exception thrown if calling pop() on a AsyncQueue or AsyncQueueDispatch object fails because the queue is empty. More... | |
class | Cgu::AsyncQueue< T, Container > |
A thread-safe asynchronous queue. More... | |
class | Cgu::AsyncQueueDispatch< T, Container > |
A thread-safe asynchronous queue with a blocking pop() method. More... | |
Namespaces | |
Cgu | |
Functions | |
template<class T , class Container > | |
void | Cgu::swap (Cgu::AsyncQueue< T, Container > &q1, Cgu::AsyncQueue< T, Container > &q2) |
template<class T , class Container > | |
void | Cgu::swap (Cgu::AsyncQueueDispatch< T, Container > &q1, Cgu::AsyncQueueDispatch< T, Container > &q2) |
This file provides thread-safe asynchronous queue classes.
AsyncQueue is a class which provides some of the functionality of a std::queue object (but note that the AsyncQueue::pop(value_type& obj) method provides the pop()ed element by reference - see the comments on that method for the reason), except that it has mutex locking of the data container so as to permit push()ing and pop()ing from different threads. It is therefore useful for passing data between threads, perhaps in response to a signal being emitted from a Notifier object.
AsyncQueueDispatch is a class which has a blocking pop() method, which allows it to be waited on by a dedicated event/message dispatching thread for incoming work (represented by the data pushed onto the queue). In the same way, it can be used to implement thread pools, by having threads in the pool waiting on the queue.
By default the queues use a std::list object as their container because when adding an item to the queue all allocation can take place outside the queue object's mutex. However, for types which have low overhead copy constructors, this can be changed to, say, a std::deque object by specifying it as the second template parameter.
If data pushed and popped from a queue are held by a reference counted smart pointer, the reference count must be thread-safe, such as by using SharedLockPtr or IntrusiveLockCounter.