Crate concurrent_queue

source ·
Expand description

A concurrent multi-producer multi-consumer queue.

There are two kinds of queues:

  1. Bounded queue with limited capacity.
  2. Unbounded queue with unlimited capacity.

Queues also have the capability to get closed at any point. When closed, no more items can be pushed into the queue, although the remaining items can still be popped.

These features make it easy to build channels similar to std::sync::mpsc on top of this crate.

Examples

use concurrent_queue::ConcurrentQueue;

let q = ConcurrentQueue::unbounded();
q.push(1).unwrap();
q.push(2).unwrap();

assert_eq!(q.pop(), Ok(1));
assert_eq!(q.pop(), Ok(2));

Features

concurrent-queue uses an std default feature. With this feature enabled, this crate will use std::thread::yield_now to avoid busy waiting in tight loops. However, with this feature disabled, core::hint::spin_loop will be used instead. Disabling std will allow this crate to be used on no_std platforms at the potential expense of more busy waiting.

There is also a portable-atomic feature, which uses a polyfill from the portable-atomic crate to provide atomic operations on platforms that do not support them. See the README for the portable-atomic crate for more information on how to use it. Note that even with this feature enabled, concurrent-queue still requires a global allocator to be available. See the documentation for the std::alloc::GlobalAlloc trait for more information.

Structs

Enums

  • Error which occurs when popping from an empty queue.
  • Error which occurs when pushing into a full or closed queue.