STTNet 0.7.0
Loading...
Searching...
No Matches
stt::system::MPSCQueue< T > Class Template Reference

Lock-free bounded MPSC queue (Multi-Producer Single-Consumer) 无锁有界多生产者单消费者队列(环形缓冲) More...

#include <sttnet.h>

Public Member Functions

 MPSCQueue (std::size_t capacity_pow2)
 
 MPSCQueue (const MPSCQueue &)=delete
 
MPSCQueueoperator= (const MPSCQueue &)=delete
 
 ~MPSCQueue ()
 
bool push (T &&v) noexcept(std::is_nothrow_move_constructible_v< T >)
 Try push (non-blocking). Returns false if queue is full. 尝试入队(非阻塞),队列满则返回 false.
 
bool push (const T &v)
 
bool pop (T &out) noexcept(std::is_nothrow_move_assignable_v< T > &&std::is_nothrow_move_constructible_v< T >)
 Try pop (single consumer). Returns false if empty. 尝试出队(单消费者),空则返回 false.
 
std::size_t approx_size () const noexcept
 Approximate size (may be inaccurate under concurrency) 近似长度(并发下可能不精确)
 
bool possibly_nonempty () const noexcept
 判断队列是否可能包含数据。
 

Detailed Description

template<typename T>
class stt::system::MPSCQueue< T >

Lock-free bounded MPSC queue (Multi-Producer Single-Consumer) 无锁有界多生产者单消费者队列(环形缓冲)

  • Multiple threads may push concurrently.
  • Only ONE thread may pop.
  • 多个线程可以同时 push
  • 仅允许一个线程 pop(通常是 reactor 线程)

Design:

  • Fixed-size ring buffer (power-of-two capacity)
  • Per-slot sequence number to coordinate producers/consumer

特点:

  • 零 malloc/零 free(不为每个元素分配 Node)
  • cache 友好
  • push/pop 只用原子 + 轻量自旋

IMPORTANT: ❗ Capacity must be a power of two. ❗ pop() must be called by only one thread.

重要: ❗ 容量必须是 2 的幂 ❗ pop 只能由一个线程调用

Constructor & Destructor Documentation

◆ MPSCQueue() [1/2]

template<typename T>
stt::system::MPSCQueue< T >::MPSCQueue ( std::size_t capacity_pow2)
inlineexplicit

◆ MPSCQueue() [2/2]

template<typename T>
stt::system::MPSCQueue< T >::MPSCQueue ( const MPSCQueue< T > & )
delete

◆ ~MPSCQueue()

template<typename T>
stt::system::MPSCQueue< T >::~MPSCQueue ( )
inline

Member Function Documentation

◆ approx_size()

template<typename T>
std::size_t stt::system::MPSCQueue< T >::approx_size ( ) const
inlinenoexcept

Approximate size (may be inaccurate under concurrency) 近似长度(并发下可能不精确)

◆ operator=()

template<typename T>
MPSCQueue & stt::system::MPSCQueue< T >::operator= ( const MPSCQueue< T > & )
delete

◆ pop()

template<typename T>
bool stt::system::MPSCQueue< T >::pop ( T & out)
inlinenoexcept

Try pop (single consumer). Returns false if empty. 尝试出队(单消费者),空则返回 false.

◆ possibly_nonempty()

template<typename T>
bool stt::system::MPSCQueue< T >::possibly_nonempty ( ) const
inlinenoexcept

判断队列是否可能包含数据。

Note
该结果用于唤醒/调度提示,不应用作并发正确性的唯一依据;真正消费仍以 pop() 为准。

◆ push() [1/2]

template<typename T>
bool stt::system::MPSCQueue< T >::push ( const T & v)
inline

◆ push() [2/2]

template<typename T>
bool stt::system::MPSCQueue< T >::push ( T && v)
inlinenoexcept

Try push (non-blocking). Returns false if queue is full. 尝试入队(非阻塞),队列满则返回 false.


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