m07-concurrency by actionbook/rust-skills
npx skills add https://github.com/actionbook/rust-skills --skill m07-concurrency第一层:语言机制
这是 CPU 密集型还是 I/O 密集型,以及共享模型是什么?
在选择并发原语之前:
| 错误 | 不要只说 | 而是应该问 |
|---|---|---|
| E0277 Send | "添加 Send 约束" | 此类型是否需要跨线程? |
| E0277 Sync | "用 Mutex 包装" | 真的需要共享访问吗? |
| Future not Send | "使用 spawn_local" | 异步是正确的选择吗? |
| 死锁 | "重新排序锁" | 锁的设计正确吗? |
在添加并发之前:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
共享模型是什么?
Send/Sync 要求是什么?
关键 : 不要仅仅修复错误。向上追溯以找到领域约束。
| 上下文关键词 | 加载领域技能 | 关键约束 |
|---|---|---|
| Web API, HTTP, axum, actix, handler | domain-web | 处理器可以在任何线程上运行 |
| 交易, 支付, trading, payment | domain-fintech | 审计 + 线程安全 |
| gRPC, kubernetes, microservice | domain-cloud-native | 分布式追踪 |
| CLI, terminal, clap | domain-cli | 通常单线程即可 |
"Rc cannot be sent between threads" in Web API context
↑ DETECT: "Web API" → Load domain-web
↑ FIND: domain-web says "Shared state must be thread-safe"
↑ FIND: domain-web says "Rc in state" is Common Mistake
↓ DESIGN: Use Arc<T> with State extractor
↓ IMPL: axum::extract::State<Arc<AppConfig>>
"Send not satisfied for my type"
↑ Ask: What domain is this? Load domain-* skill
↑ Ask: Does this type need to cross thread boundaries?
↑ Check: m09-domain (is the data model correct?)
| 情况 | 追溯到 | 问题 |
|---|---|---|
| Web 中的 Send/Sync | domain-web | 状态管理模式是什么? |
| CLI 中的 Send/Sync | domain-cli | 真的需要多线程吗? |
| Mutex vs channels | m09-domain | 共享状态还是消息传递? |
| Async vs threads | m10-performance | 工作负载概况是什么? |
从设计到实现:
"Need parallelism for CPU work"
↓ Use: std::thread or rayon
"Need concurrency for I/O"
↓ Use: async/await with tokio
"Need to share immutable data across threads"
↓ Use: Arc<T>
"Need to share mutable data across threads"
↓ Use: Arc<Mutex<T>> or Arc<RwLock<T>>
↓ Or: channels for message passing
"Need simple atomic operations"
↓ Use: AtomicBool, AtomicUsize, etc.
| 标记 | 含义 | 示例 |
|---|---|---|
Send | 可以在线程间转移所有权 | 大多数类型 |
Sync | 可以在线程间共享引用 | Arc<T> |
!Send | 必须保持在单个线程 | Rc<T> |
!Sync | 不能跨线程共享引用 | RefCell<T> |
| 模式 | 线程安全 | 阻塞 | 使用场景 |
|---|---|---|---|
std::thread | 是 | 是 | CPU 密集型并行 |
async/await | 是 | 否 | I/O 密集型并发 |
Mutex<T> | 是 | 是 | 共享可变状态 |
RwLock<T> | 是 | 是 | 读多写少的共享状态 |
mpsc::channel | 是 | 可选 | 消息传递 |
Arc<Mutex<T>> | 是 | 是 | 跨线程共享可变状态 |
What type of work?
├─ CPU-bound → std::thread or rayon
├─ I/O-bound → async/await
└─ Mixed → hybrid (spawn_blocking)
Need to share data?
├─ No → message passing (channels)
├─ Immutable → Arc<T>
└─ Mutable →
├─ Read-heavy → Arc<RwLock<T>>
└─ Write-heavy → Arc<Mutex<T>>
└─ Simple counter → AtomicUsize
Async context?
├─ Type is Send → tokio::spawn
├─ Type is !Send → spawn_local
└─ Blocking code → spawn_blocking
| 错误 | 原因 | 修复 |
|---|---|---|
E0277 Send 不满足 | 异步中的非 Send 类型 | 使用 Arc 或 spawn_local |
E0277 Sync 不满足 | 共享的非 Sync 类型 | 用 Mutex 包装 |
| 死锁 | 锁顺序问题 | 一致的锁顺序 |
future is not Send | 跨越 await 的非 Send 类型 | 在 await 前释放 |
MutexGuard 跨越 await | 挂起期间持有守卫 | 正确限定守卫作用域 |
| 反模式 | 为何不好 | 更好的做法 |
|---|---|---|
| 到处使用 Arc<Mutex> | 争用、复杂性 | 消息传递 |
| 在 async 中使用 thread::sleep | 阻塞执行器 | tokio::time::sleep |
| 在 await 期间持有锁 | 阻塞其他任务 | 严格限定锁的作用域 |
| 忽略死锁风险 | 难以调试 | 锁顺序,try_lock |
// Bad: guard held across await
let guard = mutex.lock().await;
do_async().await; // guard still held!
// Good: scope the lock
{
let guard = mutex.lock().await;
// use guard
} // guard dropped
do_async().await;
// Rc is !Send, can't cross await in spawned task
// Option 1: use Arc instead
// Option 2: use spawn_local (single-thread runtime)
// Option 3: ensure Rc is dropped before .await
| 时机 | 查看 |
|---|---|
| 智能指针选择 | m02-resource |
| 内部可变性 | m03-mutability |
| 性能调优 | m10-performance |
| 领域并发需求 | domain-* |
每周安装次数
41
仓库
GitHub 星标数
809
首次出现
2026年1月23日
安全审计
安装于
gemini-cli38
codex37
opencode36
claude-code32
github-copilot32
cursor31
GSAP 框架集成指南:Vue、Svelte 等框架中 GSAP 动画最佳实践
2,800 周安装
React TanStack 高级开发者技能指南:状态管理、路由、表单与项目架构最佳实践
98 周安装
Claude本地分析工具:跨项目隐私安全数据分析与性能监控
98 周安装
Gemini CLI 技能:本地命令行工具,集成Google Gemini AI进行代码分析、头脑风暴与安全沙箱执行
98 周安装
CLAUDE.md 架构师技能:为软件项目生成和优化 AI 项目指令文件,提升 Claude 代码效率
98 周安装
PlantUML 语法参考大全:15+图表类型快速上手,从UML到C4架构图
98 周安装
React 19、Next.js 16、Vue 3.5 前端开发专家 - 现代Web应用与组件架构模式
98 周安装