Expand description
§turbo_mpmc - High-Performance Lock-Free MPMC Queue
A blazingly fast, lock-free Multi-Producer Multi-Consumer (MPMC) queue implementation
based on Dmitry Vyukov’s bounded MPMC queue design. This implementation uses a ticket-based
system with fetch_add operations and includes batch APIs to amortize atomic operations
for maximum throughput.
§Features
- Lock-Free: Uses only atomic operations, no mutexes or locks
- MPMC: Supports multiple producers and consumers simultaneously
- Cache-Optimized: Cache-line aligned slots (64 bytes) to prevent false sharing
- Batch Operations: Send/receive multiple items with a single atomic reservation
- Zero-Copy: Efficient memory management with minimal overhead
- Type-Safe: Compile-time guarantees through Rust’s type system
§Performance Characteristics
- Single-item operations: ~10-30ns per operation
- Batch operations: ~5-15ns per item (amortized)
- Contention handling: Adaptive backoff with spin-then-yield strategy
§Quick Start
use turbo_mpmc::Queue;
use std::sync::Arc;
use std::thread;
// Create a queue with 16 slots (must be power of 2)
let queue = Arc::new(Queue::<String, 16>::new());
let producer = {
let q = queue.clone();
thread::spawn(move || {
q.send("Hello from producer!".to_string());
})
};
let consumer = {
let q = queue.clone();
thread::spawn(move || {
let msg = q.recv();
println!("Received: {}", msg);
})
};
producer.join().unwrap();
consumer.join().unwrap();§Batch Operations
For maximum throughput when sending/receiving multiple items, use the batch APIs:
use turbo_mpmc::Queue;
let queue = Queue::<i32, 64>::new();
// Send multiple items in one atomic operation
queue.send_batch(vec![1, 2, 3, 4, 5]);
// Receive multiple items in one atomic operation
let items = queue.recv_batch(5);
assert_eq!(items, vec![1, 2, 3, 4, 5]);§Architecture
The queue uses a circular buffer with atomic sequence numbers for synchronization:
- Each slot has a sequence number indicating its state (writable/readable)
- Producers acquire tickets via
fetch_addon the tail counter - Consumers acquire tickets via
fetch_addon the head counter - Cache-line alignment (64 bytes) prevents false sharing between slots and counters
§Capacity Requirements
The capacity (CAP) must be:
- Greater than zero
- A power of two (for efficient modulo operations using bitwise AND)
ⓘ
use turbo_mpmc::Queue;
// This will panic - capacity must be power of 2
let queue = Queue::<i32, 10>::new();