|
Gin
|
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. | |
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:
| T | The type of objects to store in the queue. Must be copyable. |
| LockFreeQueue< T >::LockFreeQueue | ( | int | capacity = 128 | ) |
Creates a lock-free queue with the specified capacity.
| capacity | The maximum number of items the queue can hold (default: 128) |
| 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.
| capacity | The new maximum number of items the queue can hold |
|
noexcept |
Returns the number of free slots available for writing.
|
noexcept |
Returns the number of items ready to be read.
|
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.
| bool LockFreeQueue< T >::write | ( | T | item | ) |
Writes an item to the queue.
This should only be called from the producer thread.
| item | The item to write to the queue |
| 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.
| std::optional< T > LockFreeQueue< T >::read | ( | ) |
Reads and removes the next item from the queue.
This should only be called from the consumer thread.
| 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.
| num | The number of items to remove from the queue |