Skip to main content

Codecov CI Test Safety Test no_std Test Crates.io Docs.rs PyPI version

nblf-queue

Non-Blocking Lock-Free Queue

An atomic lock-free MPMC queue based on the NBLFQ algorithm.

This repository provides multiple queue implementations with different storage and allocation strategies.

All queues in this repository are safe to use in a concurrent context and will never block the calling thread.

Queue variants

  • Static queues: fixed-capacity queues backed by static storage.
  • Allocated queues: fixed-capacity queues backed by dynamically allocated storage, only available on feature alloc.
  • Dynamic queues: dynamically resizeable queues, only available on feature dynamic.
  • Pooled Queues: variants of other queues, which may store arbitrary types, only available on feature pool.

Non-pooled queues store items in atomically updated slots, restricting the stored items to small, pointer-like values.

Usage

nblf_queue::StaticQueue:

  use nblf_queue::{StaticQueue, MPMCQueue};

  let q: StaticQueue<_, 2> = StaticQueue::new();

  assert!(q.push(&42).is_ok());
  assert!(q.push(&1).is_ok());
  assert!(q.push(&4242).is_err());

  assert_eq!(q.pop(), Some(&42));
  assert_eq!(q.pop(), Some(&1));
  assert!(q.pop().is_none());

nblf_queue::PooledStaticQueue:

  #[cfg(feature = "pool")]
  fn run() {
    use nblf_queue::{PooledStaticQueue, MPMCQueue};

    let q: PooledStaticQueue<_, 2> = PooledStaticQueue::new();

    assert!(q.push(42).is_ok());
    assert!(q.push(1).is_ok());
    assert!(q.push(4242).is_err());

    assert_eq!(q.pop(), Some(42));
    assert_eq!(q.pop(), Some(1));
    assert!(q.pop().is_none());
  }

  #[cfg(feature = "pool")]
  run();

nblf_queue::DynamicQueue:

  #[cfg(feature = "dynamic")]
  fn run() {
    use nblf_queue::{DynamicQueue, MPMCQueue, Resize};

    let q = DynamicQueue::new(1);

    assert!(q.push(42).is_ok());
    assert!(q.push(4242).is_err());

    assert!(q.resize(2));
    assert_eq!(q.capacity(), 2);
    assert!(q.push(4242).is_ok());

    assert_eq!(q.pop(), Some(42));
    assert_eq!(q.pop(), Some(4242));
    assert!(q.pop().is_none());
  }

  #[cfg(feature = "dynamic")]
  run();

Choosing a queue type

StaticQueue and Queue may only store small values and are optimized for this use case.

PooledStaticQueue and PooledQueue may store arbitrary types, at the cost of higher memory usage and runtime cost.

DynamicQueue and PooledDynamicQueue may be resized dynamically, at the cost of higher total memory usage and runtime cost. This cost is even higher for PooledDynamicQueue.

Platform Support

Multiple storage types are available, dependent on platform:

  • Tagged64 - platforms with native 64-bit atomic operations or feature atomic-fallback.

  • Tagged128 - platforms with native 128-bit atomic operations or feature atomic-fallback.

Storage types will be chosen automatically, unless sepcified explicitly.

[!NOTE] ABA Safety & Storage Selection If it is plausible that other threads could perform (2^15 - 1) * queue_size pop and push operations while a single thread is paused/preempted in pop/push, Tagged128 slots should be used to ensure ABA safety.

Feature Flags

  • std: Enables std and alloc support.

  • alloc: Enables alloc support, allowing usage of some dynamically allocated queues.

  • pool: Enables pooled queues, which may store any type.

  • dynamic: Enables dynamic queues, which may be dynamically resized. Depends on alloc.

  • atomic-fallback: Uses portable-atomic fallback feature for atomics if necessary. It is discouraged to use this feature, as fallback internally uses locks.

  • default: pool

Python Bindings

Python bindings backed by PooledQueue and PooledDynamicQueue are available for concurrent applications. Core operations detach from the GIL to allow parallel execution.

[!NOTE] The Python bindings strictly use Auto slots without feature atomic-fallback. As a result, these bindings are only supported on platforms with native 64-bit or 128-bit atomic operations.

  from nblf_queue import Queue, DynamicQueue

  q: Queue[int] = Queue(10)

  assert q.push(42) is None
  item = q.pop()
  assert item == 42

  dq: DynamicQueue[str] = DynamicQueue(1)

  assert dq.push("hello") is None
  assert dq.resize(42)
  assert dq.push("world") is None

Testing

The core test-suite of this crate was adapted from crossbeam-queue.

Current testing is based on:

  • Miri - to validate pointer arithmetic and catch UB.
  • Loom and Shuttle - to test for race conditions.
  • ASan - to check for memory corruption.

References

Alexandre Denis, Charles Goedefroit. NBLFQ: a lock-free MPMC queue optimized for low contention. IPDPS 2025 - 39th International Parallel & Distributed Processing Symposium, IEEE, Jun 2025, Milan, Italy. hal-04851700v2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nblf_queue-0.2.1.tar.gz (523.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nblf_queue-0.2.1-cp312-abi3-win_amd64.whl (125.4 kB view details)

Uploaded CPython 3.12+Windows x86-64

nblf_queue-0.2.1-cp312-abi3-macosx_11_0_arm64.whl (228.3 kB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

nblf_queue-0.2.1-cp312-abi3-macosx_10_12_x86_64.whl (232.8 kB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

nblf_queue-0.2.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (275.2 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

File details

Details for the file nblf_queue-0.2.1.tar.gz.

File metadata

  • Download URL: nblf_queue-0.2.1.tar.gz
  • Upload date:
  • Size: 523.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nblf_queue-0.2.1.tar.gz
Algorithm Hash digest
SHA256 83ce550dd88e407f48f23e012263b30e2ec2a998b9dba8c449e6a75c20f72a02
MD5 1a05ed3f68f538dcb447ef8dca3a1aa0
BLAKE2b-256 035ab4ec0522c96f9288480802aa764e11e61379c64c0490dfdf020fdc17b7b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.1.tar.gz:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nblf_queue-0.2.1-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: nblf_queue-0.2.1-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 125.4 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nblf_queue-0.2.1-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0b2083a6687f525b9e1079f64fcc2d6d0e4b48bbc905fc82a0ecfcce7d4a0fa4
MD5 99b21e5737648ab208bb7feb429cfedb
BLAKE2b-256 9c2fdee0f35fd6ba0bb5bdc171e5ea7701d76a6ca38dedd85d2cc99b18ab7856

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.1-cp312-abi3-win_amd64.whl:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nblf_queue-0.2.1-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nblf_queue-0.2.1-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6e1a8ed613e7a76ac4386e09377e302611a88eaa162e8fd72ad7acea1d891282
MD5 4d0da1ee6693fcffcf46ef58351e283c
BLAKE2b-256 6f1d89cfc71eab8e3acd46799141664139ca8aa8c155fc54ddeeb4217da716dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.1-cp312-abi3-macosx_11_0_arm64.whl:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nblf_queue-0.2.1-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nblf_queue-0.2.1-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d6c98957f4e1eae8d43818a95c0d994c23667d3ef32190db40b47ad80465e925
MD5 07dea07abfd96bce3e46f30b05c110cd
BLAKE2b-256 ee18adeb8c2ddbe65196db67c1f4f42357bb2a042fec4e6120455a11a00a4236

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.1-cp312-abi3-macosx_10_12_x86_64.whl:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nblf_queue-0.2.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nblf_queue-0.2.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b29fe01b15020a6efb60aaac66d870d074483772cc4a6eaf0dc76f93b837b074
MD5 1e64b8292ad1342bd0b6e59ef0b1a88f
BLAKE2b-256 ad5146d1fadc3f74383c70cf3b3d15b62b4caff1eed68702e80934e68a93e12b

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: pypi_release.yml on lmeller-git/nblf-queue

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page