rust-concurrency-primitives
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Concurrency Primitives
Rust并发原语
Use this skill to design thread-based Rust concurrency with explicit ownership,
sharing, and synchronization. Prefer the simplest primitive that matches the
coordination requirement before adding shared mutable state.
使用本技能,基于明确的所有权、共享与同步规则设计基于线程的Rust并发实现。在引入共享可变状态前,优先选择符合协作需求的最简原语。
Core Workflow
核心工作流程
- Classify the work: independent fork-join, producer/consumer pipeline, shared state, one-time initialization, or low-level atomic coordination.
- Prefer owned data per thread. Add shared ownership only when data must be observed or mutated by multiple threads.
- Use when child threads can borrow stack data and must finish before the function returns.
std::thread::scope - Use channels for ownership transfer and pipelines. Use or
Arc<Mutex<T>>only when shared state is the clearer model.Arc<RwLock<T>> - Keep lock scopes short and never call user-controlled or blocking code while holding a lock unless that is the invariant being protected.
- Use or
OnceLockfor thread-safe one-time initialization instead of ad hoc global mutable state.LazyLock - Treat atomics as a specialized tool. Use by default until a weaker ordering is justified and documented.
SeqCst
- 对工作进行分类:独立分叉合并、生产者/消费者流水线、共享状态、一次性初始化或底层原子协作。
- 优先为每个线程使用自有数据。仅当数据必须被多线程观察或修改时,才添加共享所有权。
- 当子线程可以借用栈数据且必须在函数返回前完成时,使用。
std::thread::scope - 使用通道进行所有权转移和流水线处理。仅当共享状态是更清晰的模型时,才使用或
Arc<Mutex<T>>。Arc<RwLock<T>> - 保持锁的作用域简短,除非要保护的不变量要求,否则持有锁时绝不要调用用户控制的代码或阻塞代码。
- 使用或
OnceLock实现线程安全的一次性初始化,而非临时的全局可变状态。LazyLock - 将原子类型视为专用工具。默认使用,直到有充分理由并记录说明需要更弱的内存顺序。
SeqCst
Primitive Selection
原语选择
Read before introducing a new shared
state primitive or reviewing deadlock-prone code.
references/threading-shared-state.md| Need | Primitive |
|---|---|
| Borrow local data into short-lived threads | |
| Transfer work or results | |
| Shared read/write state | |
| Many readers, rare writers | |
| Wait for condition changes | |
| One-time global initialization | |
| Counters, flags, lock-free coordination | |
在引入新的共享状态原语或评审易出现死锁的代码前,请阅读。
references/threading-shared-state.md| 需求 | 原语 |
|---|---|
| 将本地数据借用至短期线程 | |
| 传递任务或结果 | |
| 共享读写状态 | |
| 多读取、少写入 | |
| 等待条件变化 | 搭配 |
| 全局一次性初始化 | |
| 计数器、标志、无锁协作 | |
Safety And Review Rules
安全与评审规则
- Require for values crossing thread boundaries and
Sendfor shared references used from multiple threads.Sync - Decide whether poisoning should propagate panic or recover with
.
PoisonError::into_inner - Establish a lock ordering when more than one lock can be acquired.
- Prefer over
Arc::clone(&value)when the cloned value is an ownership handle and readability matters.value.clone() - Use Rayon for data parallel iteration when the problem is pure CPU data parallelism and the project already accepts that dependency.
- 要求跨线程边界的值实现,多线程使用的共享引用实现
Send。Sync - 决定是否让锁中毒传播恐慌,或是使用恢复。
PoisonError::into_inner - 当需要获取多个锁时,确立锁的获取顺序。
- 当克隆的是所有权句柄且可读性很重要时,优先使用而非
Arc::clone(&value)。value.clone() - 当问题属于纯CPU数据并行且项目已引入该依赖时,使用Rayon进行数据并行迭代。
Tests
测试
- Add deterministic tests around final state, message counts, and shutdown.
- Use barriers, channels, or scoped threads to coordinate tests; avoid sleeps.
- Add at least one test for panic, dropped sender/receiver, or cancellation behavior when the code depends on it.
- 围绕最终状态、消息数量和关闭逻辑添加确定性测试。
- 使用屏障、通道或作用域线程协调测试;避免使用睡眠等待。
- 当代码依赖于恐慌、发送者/接收者被丢弃或取消行为时,至少添加一个相关测试。