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.0.tar.gz (522.9 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.0-cp312-abi3-win_amd64.whl (125.4 kB view details)

Uploaded CPython 3.12+Windows x86-64

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

Uploaded CPython 3.12+macOS 11.0+ ARM64

nblf_queue-0.2.0-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.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (275.1 kB view details)

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

File details

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

File metadata

  • Download URL: nblf_queue-0.2.0.tar.gz
  • Upload date:
  • Size: 522.9 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.0.tar.gz
Algorithm Hash digest
SHA256 06af93aa75347557aeba4d4aa30e72792c1cfc8fba16db1ac5ecfc33c484b080
MD5 5a286bc40ea4e994fbeb3f8b418a907e
BLAKE2b-256 85dfa35695e48f919ae447aaee4babc00c8f22d869d9a0685065d9e6354c2012

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.0.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.0-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: nblf_queue-0.2.0-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.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d05925ff981061446500f52a77d2303c0bc89601ac750c1cd4bd7b6ced81f751
MD5 d4b5da0f14255c5cf4622bf2b0751d42
BLAKE2b-256 795e30abe2b0b89da8d34ccc30682cb8601a896573e926a552539aea831eddfe

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.0-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.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nblf_queue-0.2.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a712db2043c17425282e34054a1f17425f339c70ebe70e76b7de8694c275c5ae
MD5 f6806df75cde393fdf81052007207b66
BLAKE2b-256 b6baf90511aac408c18eb8d6c9fbb328a645cc7a75a1d80f32995d10e7feb453

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.0-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.0-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nblf_queue-0.2.0-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3e1a6c4fac4d7092bb5c30778f63057b9154b82b5feda39668b867fe92f4d066
MD5 69118b3871c49c4922cac085788661f4
BLAKE2b-256 5442daa1c5cb0a32729aa91c419a3cb1455184ee2442d30686bbccb51a55d329

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.0-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.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nblf_queue-0.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5624e4fa739ce08f2c9a9a149522853c169de3b491b3617660886f06f654959b
MD5 04cfbe8ab75d49e0dc434569cac018e7
BLAKE2b-256 65bae3284b5eb485b941526b3b7a2879d4afe35ef86268a1fbbea01cb7da02c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for nblf_queue-0.2.0-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