MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFlux::Memory::LockFreePolicy Struct Reference

Lock-free SPSC (Single Producer Single Consumer) concurrency. More...

#include <RingBuffer.hpp>

+ Collaboration diagram for MayaFlux::Memory::LockFreePolicy:

Classes

struct  State
 

Static Public Attributes

static constexpr bool is_thread_safe = true
 
static constexpr bool requires_trivial_copyable = false
 
static constexpr bool requires_fixed_storage = true
 

Detailed Description

Lock-free SPSC (Single Producer Single Consumer) concurrency.

Implements wait-free push and lock-free pop using C++20 atomic operations with careful memory ordering. Designed for real-time producer threads writing to non-realtime consumer threads.

Thread Safety:

  • ONE writer thread (producer)
  • ONE reader thread (consumer)
  • NO mutex locks (real-time safe)
  • Cache-line aligned atomics prevent false sharing

Memory Ordering:

  • relaxed: Initial atomic read (no synchronization needed)
  • acquire: Read from other thread (synchronizes-with release)
  • release: Write for other thread (synchronizes-with acquire)

SPSC Safety with Non-Trivial Types: Unlike MPMC (multi-producer multi-consumer), SPSC queues are safe for non-trivially-copyable types because:

  • Each slot written by ONLY ONE thread (producer)
  • Each slot read by ONLY ONE thread (consumer)
  • No concurrent access to same slot (guaranteed by SPSC protocol)
  • Assignment buffer[i] = value is sequentially consistent per slot

This means InputValue with std::string/std::vector is SAFE here. The atomic indices synchronize which slots are readable/writable, but the actual data copy happens in exclusive ownership.

Requirements:

  • Storage MUST be FixedStorage (compile-time capacity)
  • Capacity MUST be power of 2
  • SPSC only (single producer, single consumer)

Performance:

  • Wait-free push: O(1) without blocking
  • Lock-free pop: O(1) without mutex
  • ~10-20 CPU cycles per operation
// Works with complex types (SPSC safe)
LockFreeQueue<InputValue, 4096> input_queue; // Contains strings/vectors
// Producer thread
void enqueue_event(const InputValue& event) {
input_queue.push(event); // Safe: exclusive write to slot
}
// Consumer thread
void process_events() {
while (auto event = input_queue.pop()) {
handle(*event); // Safe: exclusive read from slot
}
}
bool push(const T &value) noexcept(is_lock_free)
Push element into buffer.
std::optional< T > pop() noexcept(is_lock_free)
Pop element from buffer.
Policy-driven unified circular buffer implementation.

Definition at line 159 of file RingBuffer.hpp.


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