知识卡片
跨线程共享可变数据
Arc> 让多个线程持有同一数据,同时让持锁者排他修改它。Arc 对克隆和释放时的引用计数更新提供原子性,Mutex 管理对内层 T 的访问。它们分工不同,因此 Rc> 仍受 Rc 非原子计数的限制,Arc 本身也不会让任意内部修改安全。单线程无需为原子计数付出开销,简单计数有专用原子整数时可避免使用整把锁。
相关卡片 [[互斥锁守卫自动释放]]、[[跨线程的两种标记]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 无畏并发 Sharing a Mutex<T> Between Multiple Threads · 查看原书依据
Fortunately, Arc<T> is a type like Rc<T> that is safe to use in concurrent situations. The a stands for atomic, meaning it’s an atomically reference-counted type. Atomics are an additional kind of concurrency primitive that we won’t cover in detail here: see the standard library documentation for std::sync::atomic for more details.
参考来源
- 位置:《The Rust Programming Language(第2版)》第16章 无畏并发(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c16.xhtml`,块 0166)
- 结论依据:原文解释 Arc 的跨线程能力来自原子引用计数,紧邻的十线程示例用 Mutex 保护实际数据。
- 原始内容:Fortunately, Arc<T> is a type like Rc<T> that is safe to use in concurrent situations. The a stands for atomic, meaning it’s an atomically reference-counted type. Atomics are an additional kind of concurrency primitive that we won’t cover in detail here: see the standard library documentation for std::sync::atomic for more details.