#concurrent-data-structure #ring-buffer


AtomicRingBuffer is a constant-size almost lock-free concurrent ring buffer

37 releases (19 stable)

1.2.9 Oct 30, 2021
1.2.8 Mar 26, 2021
1.2.7 Dec 14, 2020
1.2.6 Nov 7, 2020
0.5.6 May 19, 2018

#108 in Concurrency

Download history 243/week @ 2023-08-18 475/week @ 2023-08-25 206/week @ 2023-09-01 201/week @ 2023-09-08 227/week @ 2023-09-15 192/week @ 2023-09-22 242/week @ 2023-09-29 181/week @ 2023-10-06 297/week @ 2023-10-13 195/week @ 2023-10-20 314/week @ 2023-10-27 185/week @ 2023-11-03 205/week @ 2023-11-10 159/week @ 2023-11-17 228/week @ 2023-11-24 357/week @ 2023-12-01

967 downloads per month


815 lines

AtomicRingBuffer / AtomicRingQueue

Build Status License Cargo Documentation

A constant-size almost lock-free concurrent ring buffer


  • fast, try_push and try_pop are O(1)
  • scales well even during heavy concurrency
  • AtomicRingBuffer has only 4 words of memory overhead (AtomicRingQueue has 6 words of overhead)
  • blocking pop supported via AtomicRingQueue
  • no memory allocations after initial creation


  • growing/shrinking is not supported
  • maximum capacity of (usize >> 16) entries
  • capacity is rounded up to the next power of 2

This queue should perform similar to mpmc but with a lower memory overhead. If memory overhead is not your main concern you should run benchmarks to decide which one to use.

Implementation details

This implementation uses two atomics to store the read_index/write_index

 Read index atomic
|                     read_index                     | r_done | r_pend |
 Write index atomic
|                     write_index                    | w_done | w_pend |
  • write_index/read_index (16bit on 32bit arch, 48bits on 64bit arch): current read/write position in the ring buffer (head and tail).
  • r_pend/w_pend (8bit): number of pending concurrent read/writes
  • r_done/w_done (8bit): number of completed read/writes.

For reading r_pend is incremented first, then the content of the ring buffer is read from memory. After reading is done r_done is incremented. read_index is only incremented if r_done is equal to r_pend.

For writing first w_pend is incremented, then the content of the ring buffer is updated. After writing w_done is incremented. If w_done is equal to w_pend then both are set to 0 and write_index is incremented.

In rare cases this can result in a race where multiple threads increment r_pend in turn and r_done never quite reaches r_pend. If r_pend == 255 or w_pend == 255 a spinloop waits it to be <255 to continue. This rarely happens in practice, that's why this is called almost lock-free.


This package provides AtomicRingBuffer without blocking pop support and AtomicRingQueue with blocking pop support.


This package depends on parking_lot for blocking support in AtomicRingQueue


To use AtomicRingBuffer, add this to your Cargo.toml:

atomicring = "1.2.9"

And something like this to your code

// create an AtomicRingBuffer with capacity of 1024 elements 
let ring = ::atomicring::AtomicRingBuffer::with_capacity(900);

// try_pop removes an element of the buffer and returns None if the buffer is empty
assert_eq!(None, ring.try_pop());
// push_overwrite adds an element to the buffer, overwriting the oldest element if the buffer is full: 
assert_eq!(Some(1), ring.try_pop());
assert_eq!(None, ring.try_pop());


Licensed under the terms of MIT license and the Apache License (Version 2.0).



~11K SLoC