hawkw / thingbuf

in-place allocation-reusing queues for Rust
MIT License
293 stars 24 forks source link

thingbuf

"I'm at the buffer pool. I'm at the MPSC channel. I'm at the combination MPSC channel and buffer pool."

crates.io Documentation Documentation (HEAD) MIT licensed Test Status Sponsor @hawkw on GitHub Sponsors

What Is It?

thingbuf is a lock-free array-based concurrent ring buffer that allows access to slots in the buffer by reference. It's also asynchronous and blocking bounded MPSC channels implemented using the ring buffer.

When Should I Use It?

When Shouldn't I Use It?

It's equally important to discuss when thingbuf should not be used. Here are some cases where you might be better off considering other options:

Terminology

This crate's API and documentation makes a distinction between the terms "queue" and "channel". The term queue will refer to the queue abstract data type in general — any first-in, first-out data structure is a queue.

The term channel will refer to a subtype of concurrent queue that also functions as a synchronization primitive. A channel is a queue which can be shared between multiple threads or asynchronous tasks, and which allows those threads or tasks to wait for elements to be added or removed from the queue.

In the Rust standard library, the std::collections::VecDeque type is an example of a queue that is not a channel: it is a first-in, first-out data structure, but it cannot be concurrently enqueued to and dequeued from by multiple threads or tasks. In comparison, the types in the std::sync::mpsc module provide a prototypical example of channels, as they serve as synchronization primitives for cross-thread communication.

Usage

To get started using thingbuf, add the following to your Cargo.toml:

[dependencies]
thingbuf = "0.1"

By default, thingbuf depends on the Rust standard library, in order to implement APIs such as synchronous (blocking) channels. In #![no_std] projects, the std feature flag must be disabled:

[dependencies]
thingbuf = { version = "0.1", default-features = false }

With the std feature disabled, thingbuf will depend only on libcore. This means that APIs that require dynamic memory allocation will not be enabled. Statically allocated channels and queues are available for code without a memory allocator, if the static feature flag is enabled:

[dependencies]
thingbuf = { version = "0.1", default-features = false, features = ["static"] }

However, if a memory allocator is available, #![no_std] code can also enable the alloc feature flag to depend on liballoc:

[dependencies]
thingbuf = { version = "0.1", default-features = false, features = ["alloc"] }

Crate Feature Flags

Compiler Support

thingbuf is built against the latest stable release. The minimum supported version is Rust 1.57. The current thingbuf version is not guaranteed to build on Rust versions earlier than the minimum supported version.

Some feature flags may require newer Rust releases. For example, the "static" feature flag requries Rust 1.60+.

FAQs