pub struct SPMCBuffer<T: Send + Sync> { /* private fields */ }Expand description
A single-producer multiple-consumer buffer, useful for thread-safe data sharing in scenarios where triple buffering won’t cut it.
Triple buffering is an extremely efficient synchronization protocol when a producer thread wants to constantly update a value that is visible by a single consumer thread. However, it is not safe to use in the presence of multiple consumers, because a consumer thread can no longer assume that it is the only thread having access to the read buffer and discard said read buffer at will.
Reference counting techniques can be used to build a variant of triple buffering which works for multiple consumers, remains provably wait-free if one uses two buffers per consumer, and degrades gracefully when a smaller amount of buffers is used as long as consumers frequently fetch updates from the producer. I call the resulting synchronization primitive an SPMC buffer.
Implementations§
Source§impl<T: Default + Send + Sync> SPMCBuffer<T>
impl<T: Default + Send + Sync> SPMCBuffer<T>
Sourcepub fn with_default(read_buffers: usize) -> Self
pub fn with_default(read_buffers: usize) -> Self
Like “new”, but uses default-constructed values of type T instead of cloning a user-provided initial value of this type.
Source§impl<T: Send + Sync> SPMCBuffer<T>
impl<T: Send + Sync> SPMCBuffer<T>
Sourcepub fn split(self) -> (SPMCBufferInput<T>, SPMCBufferOutput<T>)
pub fn split(self) -> (SPMCBufferInput<T>, SPMCBufferOutput<T>)
Extract input and output of the SPMC buffer