c++-gtk-utils
Classes | Namespaces | Functions
async_channel.h File Reference

This file provides a "channel" class for inter-thread communication. More...

#include <utility>
#include <new>
#include <cstddef>
#include <cstdlib>
#include <pthread.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::AsyncChannel< T, n >
 A thread-safe "channel" class for inter-thread communication. More...
 

Namespaces

 Cgu
 

Functions

void Cgu::cgu_async_channel_waiters_dec (void *arg)
 

Detailed Description

This file provides a "channel" class for inter-thread communication.

AsyncChannel is similar to the AsyncQueueDispatch class, in that it provides a means of sending data between threads. Producer threads push data onto the queue and consumer threads pop them off in a thread safe way, and if there are no data in the channel any consumer thread will block until a producer thread pushes an item onto it. However, unlike the AsyncQueueDispatch class, it has a fixed maximum size as part of its type, which may be any size greater than 0, and if the number of data items still in the channel is such as to make the channel full, then producer threads will block on the channel until a consumer thread pops an item from it.

It therefore provides for back pressure on producer threads which will automatically prevent the channel being overwhelmed by producer threads pushing more items onto the queue than consumer threads have the capacity to take off it.

AsyncChannel is useful where this feature is important, and an AsyncChannel object can be used with any number of producer threads and consumer threads. However, under heavy contention with complex data item types AsyncQueueDispatch objects will usually be faster. Under lower contention and with simpler data types (or where temporary objects with non-complex move constructors are pushed), an AsyncChannel object may be faster as it can benefit from its fixed buffer size (the AsyncChannel implementation uses a circular buffer with in-buffer construction of data items).

This class is available from version 2.0.31.