pianod2
multisource multiuser scriptable networked music player
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
ThreadSafeCircularQueue< ValueType, capacity > Class Template Reference

An circular queue meant for sharing data across threads. More...

#include <threadsafecircularqueue.h>

Collaboration diagram for ThreadSafeCircularQueue< ValueType, capacity >:
Collaboration graph
[legend]

Classes

struct  StorageType
 

Public Types

using value_type = ValueType
 

Public Member Functions

template<typename VT >
void push_back (VT &&value)
 Add or move an item into the queue. More...
 
template<typename VT >
bool try_push_back (VT &&value, const std::chrono::milliseconds &wait_duration=std::chrono::milliseconds::zero())
 Add or move an item into the queue. More...
 
ValueType pop_front (int *overflows=nullptr, time_t *sent_time=nullptr)
 Retrieve an item from the queue. More...
 
bool try_pop_front (ValueType *value, int *overflows=nullptr, time_t *sent_time=nullptr, std::chrono::milliseconds wait_duration=std::chrono::milliseconds::zero())
 Retrieve an item from the queue. More...
 

Private Types

using index_type = uint32_t
 

Private Member Functions

index_type next (index_type index)
 Increment an index, wrapping around buffer size. More...
 
void write_common ()
 Perform actions common to the various forms of push_back. More...
 

Private Attributes

StorageType data [capacity]
 
std::atomic< index_typeread_index
 
std::mutex read_mutex
 
std::condition_variable read_bell
 
std::atomic< index_typewrite_index
 
std::mutex write_mutex
 
std::condition_variable write_bell
 

Detailed Description

template<typename ValueType, uint32_t capacity>
class ThreadSafeCircularQueue< ValueType, capacity >

An circular queue meant for sharing data across threads.

Template Parameters
ValueTypeType of objects being passed around.
capacityThe maximum capacity of the queue.

Member Typedef Documentation

◆ index_type

template<typename ValueType , uint32_t capacity>
using ThreadSafeCircularQueue< ValueType, capacity >::index_type = uint32_t
private

◆ value_type

template<typename ValueType , uint32_t capacity>
using ThreadSafeCircularQueue< ValueType, capacity >::value_type = ValueType

Member Function Documentation

◆ next()

template<typename ValueType , uint32_t capacity>
index_type ThreadSafeCircularQueue< ValueType, capacity >::next ( index_type  index)
inlineprivate

Increment an index, wrapping around buffer size.

Here is the caller graph for this function:

◆ pop_front()

template<typename ValueType , uint32_t capacity>
ValueType ThreadSafeCircularQueue< ValueType, capacity >::pop_front ( int *  overflows = nullptr,
time_t *  sent_time = nullptr 
)
inline

Retrieve an item from the queue.

If the queue is empty, block until an item becomes available.

Parameters
overflowsIf non-null, set to the number of items dropped due to overflow.
sent_timeIf non-null, the time at which the item was queued.
Returns
The next value from the queue.
Here is the call graph for this function:

◆ push_back()

template<typename ValueType , uint32_t capacity>
template<typename VT >
void ThreadSafeCircularQueue< ValueType, capacity >::push_back ( VT &&  value)
inline

Add or move an item into the queue.

If the queue is full, wait until there's space.

Parameters
valueThe value to add.
Here is the call graph for this function:

◆ try_pop_front()

template<typename ValueType , uint32_t capacity>
bool ThreadSafeCircularQueue< ValueType, capacity >::try_pop_front ( ValueType *  value,
int *  overflows = nullptr,
time_t *  sent_time = nullptr,
std::chrono::milliseconds  wait_duration = std::chrono::milliseconds::zero() 
)
inline

Retrieve an item from the queue.

If the queue is empty, don't block.

Parameters
valueOn return, set to the value retrieved from the queue.
overflowsIf non-null, set to the number of items dropped due to overflow.
sent_timeIf non-null, the time at which the item was queued.
wait_durationA duration to wait for for an item; defaults to no wait.
Returns
True if a value was retrieved, false if the queue was empty.
Here is the call graph for this function:

◆ try_push_back()

template<typename ValueType , uint32_t capacity>
template<typename VT >
bool ThreadSafeCircularQueue< ValueType, capacity >::try_push_back ( VT &&  value,
const std::chrono::milliseconds &  wait_duration = std::chrono::milliseconds::zero() 
)
inline

Add or move an item into the queue.

If the queue is full, mark that an overflow occured, and do not block.

Parameters
valueThe value to add.
wait_durationOptional: If specified, a duration to wait before giving up. Default is to not wait. Observation, however, shows that small waits can go a long way to reduce overflows, providing a chance for readers to catch up when the queue is full.
Returns
True on success, false on overflow.
Here is the call graph for this function:

◆ write_common()

template<typename ValueType , uint32_t capacity>
void ThreadSafeCircularQueue< ValueType, capacity >::write_common ( )
inlineprivate

Perform actions common to the various forms of push_back.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ data

template<typename ValueType , uint32_t capacity>
StorageType ThreadSafeCircularQueue< ValueType, capacity >::data[capacity]
private

◆ read_bell

template<typename ValueType , uint32_t capacity>
std::condition_variable ThreadSafeCircularQueue< ValueType, capacity >::read_bell
private

◆ read_index

template<typename ValueType , uint32_t capacity>
std::atomic<index_type> ThreadSafeCircularQueue< ValueType, capacity >::read_index
private

◆ read_mutex

template<typename ValueType , uint32_t capacity>
std::mutex ThreadSafeCircularQueue< ValueType, capacity >::read_mutex
private

◆ write_bell

template<typename ValueType , uint32_t capacity>
std::condition_variable ThreadSafeCircularQueue< ValueType, capacity >::write_bell
private

◆ write_index

template<typename ValueType , uint32_t capacity>
std::atomic<index_type> ThreadSafeCircularQueue< ValueType, capacity >::write_index
private

◆ write_mutex

template<typename ValueType , uint32_t capacity>
std::mutex ThreadSafeCircularQueue< ValueType, capacity >::write_mutex
private

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