Lock-free bounded MPSC queue (Multi-Producer Single-Consumer) 无锁有界多生产者单消费者队列(环形缓冲)
More...
|
| | MPSCQueue (std::size_t capacity_pow2) |
| |
| | MPSCQueue (const MPSCQueue &)=delete |
| |
| MPSCQueue & | operator= (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. More...
|
| |
| bool | push (const T &v) noexcept(std::is_nothrow_copy_constructible_v< T >) |
| |
| 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. More...
|
| |
| std::size_t | approx_size () const noexcept |
| | Approximate size (may be inaccurate under concurrency) 近似长度(并发下可能不精确) More...
|
| |
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 只能由一个线程调用