Simple Channels
A collection of simple, thread-safe channel implementations in Rust, created for educational purposes. This project focuses on demonstrating the core principles of concurrent data structures using clear, concise code.
The initial implementation is a Multi-Producer, Single-Consumer (MPSC) channel.
Core Components
-
Lock-Free
RingBuf: A fixed-size, circular buffer that uses atomic operations and a compare-and-swap (CAS) loop to manage concurrent access without mutexes. -
MPSC Channel API: A high-level
Sender/ReceiverAPI built on theRingBuf.- The
Sendercan be cloned to allow writes from multiple threads. - A single
Receiverconsumes values, enforcing the single-consumer model.
- The
Design Philosophy
The implementation prioritizes clarity and fundamental concepts over raw performance.
-
Blocking Strategy: The channel uses a simple spin-and-yield approach (
std::thread::yield_now()) for blocking. When the buffer is full or empty, threads yield their execution slot, avoiding more complex synchronization primitives likepark/unpark. -
State Management: Shared channel state is managed via
Arc, and disconnection is detected by checking the atomic reference count.
Roadmap
This project can be extended by implementing other common channel types and features. Potential future work includes:
- SPSC Channel: A specialized channel for the Single-Producer, Single-Consumer use case.
- MPMC Channel: A more complex Multi-Producer, Multi-Consumer channel.
- Unbounded Channel: A channel variant that can grow dynamically.
- Efficient Blocking: Integration with a more sophisticated mechanism like
std::thread::parkto reduce CPU usage while waiting. - Benchmarking: A benchmark suite to measure and compare performance during development.
- Testing: Try out deterministic simulation testing