Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsMay 27, 2026·3 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Crossbeam Concurrency
Comando de instalación directa
npx -y tokrepo@latest install a250a1b3-5a0a-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con 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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados