Gin
Loading...
Searching...
No Matches
Public Member Functions | List of all members
LockFreeQueue< T > Class Template Reference

A lock-free FIFO queue for single-producer, single-consumer scenarios. More...

#include <gin_lockfreequeue.h>

Public Member Functions

 LockFreeQueue (int capacity=128)
 Creates a lock-free queue with the specified capacity.
 
void setSize (int capacity)
 Resizes the queue to a new capacity.
 
int getFreeSpace () const noexcept
 Returns the number of free slots available for writing.
 
int getNumReady () const noexcept
 Returns the number of items ready to be read.
 
void reset () noexcept
 Resets the queue to an empty state.
 
bool write (T item)
 Writes an item to the queue.
 
std::optional< T > peek ()
 Peeks at the next item in the queue without removing it.
 
std::optional< T > read ()
 Reads and removes the next item from the queue.
 
bool pop (int num)
 Removes (pops) multiple items from the queue without returning them.
 

Detailed Description

template<typename T>
class LockFreeQueue< T >

A lock-free FIFO queue for single-producer, single-consumer scenarios.

This template class provides a thread-safe queue that can be used to pass objects between threads without using locks. It's built on top of JUCE's AbstractFifo and is suitable for real-time audio processing where blocking is not acceptable.

The queue uses a circular buffer internally and supports write, read, peek, and pop operations. All operations are lock-free and safe for use between one producer thread and one consumer thread.

Example usage:

LockFreeQueue<int> queue (100);
// Producer thread
queue.write (42);
queue.write (123);
// Consumer thread
auto value = queue.read();
if (value.has_value())
process (*value);
A lightweight 2D point class for projects that don't use juce_graphics.
Definition gin_point.h:25
Template Parameters
TThe type of objects to store in the queue. Must be copyable.

Constructor & Destructor Documentation

◆ LockFreeQueue()

template<typename T >
LockFreeQueue< T >::LockFreeQueue ( int  capacity = 128)

Creates a lock-free queue with the specified capacity.

Parameters
capacityThe maximum number of items the queue can hold (default: 128)

Member Function Documentation

◆ setSize()

template<typename T >
void LockFreeQueue< T >::setSize ( int  capacity)

Resizes the queue to a new capacity.

Note: This is not thread-safe and should only be called when no other threads are accessing the queue.

Parameters
capacityThe new maximum number of items the queue can hold

◆ getFreeSpace()

template<typename T >
int LockFreeQueue< T >::getFreeSpace ( ) const
noexcept

Returns the number of free slots available for writing.

Returns
The number of items that can be written before the queue is full

◆ getNumReady()

template<typename T >
int LockFreeQueue< T >::getNumReady ( ) const
noexcept

Returns the number of items ready to be read.

Returns
The number of items currently in the queue

◆ reset()

template<typename T >
void LockFreeQueue< T >::reset ( )
noexcept

Resets the queue to an empty state.

Note: This is not thread-safe and should only be called when no other threads are accessing the queue.

◆ write()

template<typename T >
bool LockFreeQueue< T >::write ( item)

Writes an item to the queue.

This should only be called from the producer thread.

Parameters
itemThe item to write to the queue
Returns
true if the write succeeded, false if the queue was full

◆ peek()

template<typename T >
std::optional< T > LockFreeQueue< T >::peek ( )

Peeks at the next item in the queue without removing it.

This should only be called from the consumer thread.

Returns
An optional containing the next item, or std::nullopt if the queue is empty

◆ read()

template<typename T >
std::optional< T > LockFreeQueue< T >::read ( )

Reads and removes the next item from the queue.

This should only be called from the consumer thread.

Returns
An optional containing the next item, or std::nullopt if the queue is empty

◆ pop()

template<typename T >
bool LockFreeQueue< T >::pop ( int  num)

Removes (pops) multiple items from the queue without returning them.

This is useful for discarding items you've already processed via peek(). This should only be called from the consumer thread.

Parameters
numThe number of items to remove from the queue
Returns
true if the pop succeeded, false if there weren't enough items

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