Configs2026年5月27日·1 分钟阅读

Crossbeam — Concurrent Programming Primitives for Rust

Crossbeam provides tools for concurrent programming in Rust, including lock-free data structures, scoped threads, channels, and epoch-based memory reclamation. It is the foundation for concurrency in many high-performance Rust applications and is used internally by Rayon and Tokio.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Crossbeam Concurrency
直接安装命令
npx -y tokrepo@latest install a250a1b3-5a0a-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Crossbeam is a collection of tools for concurrent programming in Rust. It fills gaps in the standard library with battle-tested primitives: multi-producer multi-consumer channels, lock-free queues and deques, scoped threads, and an epoch-based garbage collector for lock-free data structures. It is one of the most depended-upon crates in the Rust ecosystem.

What Crossbeam Does

  • Provides MPMC channels that are faster and more flexible than the standard library's channels
  • Offers scoped threads that can borrow from the parent stack without requiring Arc or static lifetimes
  • Implements lock-free concurrent data structures (queues, deques, stacks)
  • Includes epoch-based memory reclamation for safe lock-free programming
  • Supplies synchronization utilities like WaitGroup and ShardedLock

Architecture Overview

Crossbeam is organized as a workspace of focused sub-crates: crossbeam-channel for channels, crossbeam-deque for work-stealing deques, crossbeam-epoch for epoch-based garbage collection, crossbeam-queue for lock-free queues, and crossbeam-utils for scoped threads and other utilities. The epoch-based reclamation system allows threads to safely read shared data without locks by tracking which epochs are still active and deferring deallocation until no thread can reference the old data.

Self-Hosting & Configuration

  • Add crossbeam = "0.8" to your Cargo.toml for the full suite, or pick individual sub-crates
  • No runtime configuration needed; all primitives are initialized in code
  • Channels are created with channel::bounded(cap) or channel::unbounded() depending on backpressure needs
  • Scoped threads are created with crossbeam::scope(|s| { s.spawn(|_| { ... }); })
  • Enable only the features you need via Cargo feature flags to minimize compile time

Key Features

  • MPMC channels with select macro for waiting on multiple channels simultaneously
  • Scoped threads that safely borrow local variables without Arc or ownership transfer
  • Work-stealing deques used by Rayon for efficient parallel task scheduling
  • Epoch-based memory reclamation enabling safe lock-free data structure implementations
  • Zero unsafe code required by users; all safety invariants are handled internally

Comparison with Similar Tools

  • std::sync::mpsc — standard library channels are MPSC only and slower; Crossbeam channels are MPMC with better performance
  • Rayon — data-parallelism library built on top of Crossbeam's deques; higher-level API for parallel iterators
  • Tokio — async runtime; Crossbeam is for OS-thread concurrency, not async tasks
  • flume — alternative MPMC channel crate; simpler API but fewer features than Crossbeam's select macro
  • parking_lot — provides faster Mutex/RwLock implementations; complementary to Crossbeam's lock-free structures

FAQ

Q: When should I use Crossbeam channels vs Tokio channels? A: Use Crossbeam for synchronous (OS thread) concurrency and Tokio channels for async task communication. They serve different concurrency models.

Q: Are Crossbeam channels faster than std::sync::mpsc? A: Yes. Benchmarks consistently show Crossbeam channels outperforming the standard library's channels, especially under contention.

Q: What are scoped threads good for? A: Scoped threads let you spawn threads that borrow data from the parent scope, avoiding the need to wrap everything in Arc or clone data. The scope guarantees all threads finish before the borrowed data goes out of scope.

Q: Is Crossbeam used in production? A: Yes. It is a transitive dependency of many popular crates including Rayon, Tokio, and Servo. It has billions of downloads on crates.io.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产