crossbeam
repository·main·Indexed 27 days ago
https://github.com/crossbeam-rs/crossbeamA collection of tools for concurrent programming in Rust, providing high-performance primitives for atomics, data structures, memory management, and thread synchronization. It includes specialized subcrates such as crossbeam-channel for MPMC channels, crossbeam-deque for work-stealing deques, crossbeam-epoch for epoch-based garbage collection, crossbeam-queue for concurrent queues, crossbeam-skiplist for concurrent maps and sets, and crossbeam-utils for synchronization primitives and scoped threads.
What's inside crossbeam
- Crossbeam Epoch provides epoch-based garbage collection (GC) designed for building concurrent data structures. It solves the problem of safe memory reclamation: when a thread removes an object from a concurrent structure, other threads might still hold pointers to it. This crate provides an efficient mechanism to defer the destruction of shared objects until it is guaranteed that no pointers to them exist.
Overview of crossbeam-channel features
maincrossbeam-channelprovides high-performance message passing with the following capabilities:- MPMC Support: Both
Senders andReceivers can be cloned and shared among multiple threads. - Channel Types:
bounded: Channels with a fixed capacity.unbounded: Channels with no fixed capacity.
- Specialized Channels:
after: Provides a timer-based channel.never: A channel that can never receive a message.tick: Provides a periodic timer channel.
- Selection Mechanisms:
select!macro: Allows blocking on multiple channel operations simultaneously.Selectstruct: Allows selecting over a dynamically built list of channel operations.
- MPMC Support: Both
Overview of crossbeam-utils features
mainAtomics
AtomicCell: A thread-safe mutable memory location. (no_std)AtomicConsume: For reading from primitive atomic types with "consume" ordering. (no_std)
Thread synchronization
Parker: A thread parking primitive.ShardedLock: A sharded reader-writer lock with fast concurrent reads.WaitGroup: For synchronizing the beginning or end of some computation.
Utilities
Backoff: For exponential backoff in spin loops. (no_std)CachePadded: For padding and aligning a value to the length of a cache line. (no_std)scope: For spawning threads that borrow local variables from the stack.
Overview of Crossbeam tools
mainCrossbeam provides a suite of tools for concurrent programming, categorized by functionality:
Atomics
AtomicCell: A thread-safe mutable memory location (supportsno_std).AtomicConsume: For reading from primitive atomic types with "consume" ordering (supportsno_std).
Data structures
deque: Work-stealing deques for building task schedulers.ArrayQueue: A bounded MPMC (multi-producer multi-consumer) queue with a fixed-capacity buffer (requiresalloc).SegQueue: An unbounded MPMC queue that allocates small buffers (segments) on demand (requiresalloc).
Memory management
epoch: An epoch-based garbage collector (requiresalloc).
Thread synchronization
channel: MPMC channels for message passing.Parker: A thread parking primitive.ShardedLock: A sharded reader-writer lock optimized for fast concurrent reads.WaitGroup: Synchronizes the beginning or end of computations.
Utilities
Backoff: Exponential backoff for spin loops (supportsno_std).CachePadded: Pads and aligns values to the length of a cache line (supportsno_std).scope: Spawns threads that can safely borrow local variables from the stack.
Use ArrayQueue and SegQueue for concurrent queues
mainThe
crossbeam-queuecrate provides two types of Multi-Producer Multi-Consumer (MPMC) queues:ArrayQueue: A bounded queue that allocates a fixed-capacity buffer upon construction.SegQueue: An unbounded queue that allocates small buffers (segments) on demand.
Both types can be used in
no_stdenvironments if theallocfeature is enabled.Use SkipMap and SkipSet for concurrent access
mainThe
crossbeam-skiplistcrate providesSkipMapandSkipSet. These types offer interfaces similar toBTreeMapandBTreeSetbut are designed for safe concurrent access across multiple threads.For
no_stdenvironments that implementalloc, you must enable theallocfeature.Install crossbeam-deque
mainAdd
crossbeam-dequeto yourCargo.tomldependencies to use work-stealing deques in your project. These deques are primarily intended for building task schedulers.[dependencies] crossbeam-deque = "0.8"Install the crossbeam crate
mainTo use Crossbeam in your Rust project, add it to your
Cargo.tomldependencies file.[dependencies] crossbeam = "0.8"Run crossbeam-channel benchmarks
mainTo run the performance benchmarks for
crossbeam-channel, use the provided shell script. This will execute the benchmark tests, save the raw results into.txtfiles, and generate a visualization namedplot.png../run.shInstall crossbeam-channel
mainTo use
crossbeam-channelin your Rust project, add it to yourCargo.tomldependencies. It is an alternative tostd::sync::mpscproviding multi-producer multi-consumer (MPMC) channels with better performance and more features.[dependencies] crossbeam-channel = "0.5"Install crossbeam-utils
mainTo use
crossbeam-utilsin your project, add it to yourCargo.tomldependencies file.[dependencies] crossbeam-utils = "0.8"Install crossbeam-skiplist
mainAdd
crossbeam-skiplistto yourCargo.tomldependencies to use concurrentSkipMapandSkipSetdata structures.[dependencies] crossbeam-skiplist = "0.1"