Lock-Free SPSC Queue

Hard C++ Synchronization Asked at NVIDIA
Solve this problem

Implement a single-producer single-consumer queue that needs no mutex, using acquire/release atomics so an ISR can hand data to the main loop safely.

Constraints

  • Exactly one producer thread and one consumer thread.
  • Head and tail are std::atomic<size_t>.
  • Producer uses release on store, consumer uses acquire on load.
  • Capacity is a power of two; use masking.
  • No mutex, no CAS loop.
  • Must be correct on a weakly ordered architecture.
Starter codeC++
1#include <atomic>2#include <cstddef>3 4template <typename T, size_t Capacity>5class SpscQueue {6    // TODO7};