Struct jack::RingBuffer
source · pub struct RingBuffer(_);
Expand description
A lock-free ringbuffer. The key attribute of a ringbuffer is that it can be safely accessed by two threads simultaneously, one reading from the buffer and the other writing to it - without using any synchronization or mutual exclusion primitives. For this to work correctly, there can only be a single reader and a single writer thread. Their identities cannot be interchanged.
Example
let ringbuf = jack::RingBuffer::new(1024).unwrap();
let (mut reader, mut writer) = ringbuf.into_reader_writer();
let buf = [0_u8, 1, 2, 3];
let num = writer.write_buffer(&buf);
assert_eq!(num, buf.len());
// Potentially in a another thread:
let mut outbuf = [0_u8; 8];
let num = reader.read_buffer(&mut outbuf);
Implementations§
source§impl RingBuffer
impl RingBuffer
sourcepub fn into_reader_writer(self) -> (RingBufferReader, RingBufferWriter)
pub fn into_reader_writer(self) -> (RingBufferReader, RingBufferWriter)
Create a reader and writer, to use the ring buffer.
sourcepub fn from_reader_writer(r: RingBufferReader, w: RingBufferWriter) -> Self
pub fn from_reader_writer(r: RingBufferReader, w: RingBufferWriter) -> Self
Re-create the ring buffer object from reader and writer. useful if you need to call reset. The reader and the writer pair must have been created from the same RingBuffer object. Not needed for deallocation, disposing of both reader and writer will deallocate buffer resources automatically.
panics if the reader and the writer were created from different RingBuffer objects.