#blocking #spsc #ring-buffer #rb #fifo

no-std ringbuf-blocking

Blocking version of ringbuf

4 releases

0.1.0-rc.3 Feb 11, 2024
0.1.0-rc.2 Aug 31, 2023
0.1.0-rc.1 Aug 21, 2023
0.1.0-rc.0 Jun 13, 2023

#149 in Concurrency

Download history 9/week @ 2023-10-31 6/week @ 2023-11-07 3/week @ 2023-11-14 13/week @ 2023-11-21 14/week @ 2023-11-28 4/week @ 2023-12-05 6/week @ 2023-12-12 6/week @ 2023-12-19 10/week @ 2023-12-26 6/week @ 2024-01-02 4/week @ 2024-01-09 3/week @ 2024-01-16 3/week @ 2024-01-23 7/week @ 2024-01-30 30/week @ 2024-02-06 51/week @ 2024-02-13

91 downloads per month
Used in flatty-io




Crates.io Docs.rs Gitlab CI License

Lock-free SPSC FIFO ring buffer with direct access to inner data.


  • Lock-free operations - they succeed or fail immediately without blocking or waiting.
  • Arbitrary item type (not only Copy).
  • Items can be inserted and removed one by one or many at once.
  • Thread-safe direct access to the internal ring buffer memory.
  • Read and Write implementation.
  • Overwriting insertion support.
  • Different types of buffers and underlying storages.
  • Can be used without std and even without alloc (using only statically-allocated memory).
  • Async and blocking versions (see this section).


At first you need to create the ring buffer itself. HeapRb is recommended but you may choose another one.

After the ring buffer is created it may be splitted into pair of Producer and Consumer. Producer is used to insert items to the ring buffer, consumer - to remove items from it.


There are several types of ring buffers provided:

  • LocalRb. Only for single-threaded use.
  • SharedRb. Can be shared between threads. Its frequently used instances:
    • HeapRb. Contents are stored in dynamic memory. Recommended for use in most cases.
    • StaticRb. Contents can be stored in statically-allocated memory.

You may also provide your own generic parameters.


SharedRb needs to synchronize CPU cache between CPU cores. This synchronization has some overhead. To avoid multiple unnecessary synchronizations you may use methods that operate many items at once (push_slice/push_iter, pop_slice/pop_iter, etc.) or you can freeze producer or consumer and then synchronize threads manually (see items in frozen module).

For single-threaded usage LocalRb is recommended because it is slightly faster than SharedRb due to absence of CPU cache synchronization.



use ringbuf::{traits::*, HeapRb};

let rb = HeapRb::<i32>::new(2);
let (mut prod, mut cons) = rb.split();

assert_eq!(prod.try_push(2), Err(2));

assert_eq!(cons.try_pop(), Some(0));


assert_eq!(cons.try_pop(), Some(1));
assert_eq!(cons.try_pop(), Some(2));
assert_eq!(cons.try_pop(), None);

No heap

use ringbuf::{traits::*, StaticRb};

const RB_SIZE: usize = 1;
let mut rb = StaticRb::<i32, RB_SIZE>::default();
let (mut prod, mut cons) = rb.split_ref();

assert_eq!(prod.try_push(123), Ok(()));
assert_eq!(prod.try_push(321), Err(321));

assert_eq!(cons.try_pop(), Some(123));
assert_eq!(cons.try_pop(), None);


Ring buffer can be used in overwriting mode when insertion overwrites the latest element if the buffer is full.

use ringbuf::{traits::*, HeapRb};

let mut rb = HeapRb::<i32>::new(2);

assert_eq!(rb.push_overwrite(0), None);
assert_eq!(rb.push_overwrite(1), None);
assert_eq!(rb.push_overwrite(2), Some(0));

assert_eq!(rb.try_pop(), Some(1));
assert_eq!(rb.try_pop(), Some(2));
assert_eq!(rb.try_pop(), None);

Note that push_overwrite requires exclusive access to the ring buffer so to perform it concurrently you need to guard the ring buffer with mutex or some other lock.

Derived crates


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.