pub struct Consumer<T: AnyBitPattern, const CAPACITY: usize> { /* private fields */ }Expand description
A reading handle of an SPMC broadcast queue.
Each consumer has its own private read cursor: every consumer observes
every value the producer publishes, independently, unless it falls behind
by more than CAPACITY slots.
Consumer is Send but not Sync: at most one thread may call
try_read on a given consumer at a time. To get more
consumers, request more at construction via new.
Implementations§
Source§impl<T, const CAPACITY: usize> Consumer<T, CAPACITY>where
T: AnyBitPattern,
impl<T, const CAPACITY: usize> Consumer<T, CAPACITY>where
T: AnyBitPattern,
Sourcepub fn try_read(&mut self) -> ReadResult<T>
pub fn try_read(&mut self) -> ReadResult<T>
Attempts to read the next value.
Wait-free in the common case (value available, or queue empty); lock-free when the producer is actively writing the target slot (the consumer spin-loops until the producer publishes).
§Protocol
Each slot carries an even/odd seq number: even = stable, odd = mid write. The consumer:
- Loads the slot’s seq. If odd, spin until it goes even.
- If the even seq is not the one expected for the current read
cursor, the producer has lapped us; reload the write cursor, jump
to the most recent slot, and return
ReadResult::Lapped. - Otherwise read the slot’s data, then re-load the seq. If the seq
is unchanged the read is committed and a
ReadResult::Valueis returned; if the seq has moved, the read was torn and the loop retries.
§Examples
use low_latency_data_structures::spmc::{ReadResult, new};
let (producer, [mut consumer]) = new::<u64, 16, 1>();
assert_eq!(consumer.try_read(), ReadResult::Empty);
producer.publish(7);
assert_eq!(consumer.try_read(), ReadResult::Value(7));Trait Implementations§
impl<T: AnyBitPattern, const CAPACITY: usize> Send for Consumer<T, CAPACITY>
Auto Trait Implementations§
impl<T, const CAPACITY: usize> !RefUnwindSafe for Consumer<T, CAPACITY>
impl<T, const CAPACITY: usize> !Sync for Consumer<T, CAPACITY>
impl<T, const CAPACITY: usize> !UnwindSafe for Consumer<T, CAPACITY>
impl<T, const CAPACITY: usize> Freeze for Consumer<T, CAPACITY>
impl<T, const CAPACITY: usize> Unpin for Consumer<T, CAPACITY>
impl<T, const CAPACITY: usize> UnsafeUnpin for Consumer<T, CAPACITY>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more