知识卡片

普通读书笔记卡 · 6074.e.1.b.5

共享拥有期与互斥责任分开

理解等级:中级

Arc维持多个线程共同使用对象时的拥有期,Mutex保护并发访问,两者解决不同问题。每线程克隆Arc并移入闭包,可避免借用局部账户超过其有效期。只加move会把唯一账户交给首个线程,只加锁也不能自动延长局部变量寿命。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 3. Concepts Data races in Rust · 查看原书依据

      Arc<Mutex<T>> is common in Rust programs that use shared-state parallelism. Stepping back from the technical details, observe that Rust has entirely avoided the problem of data races that plagues multithreaded programming in other languages. Of course, this good news is restricted to safe Rust—unsafe code (Item 16) and FFI boundaries in particular (Item 34)

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Data races in Rust》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u102-ch03-035)。 - 结论依据:原文在“Arc<Mutex<T>>”附近的定义、例子或代码支撑本卡核心说明:Arc维持多个线程共同使用对象时的拥有期,Mutex保护并发访问,两者解决不同问题。边界按已读前提解释;示例未执行。 - 原始内容:Arc<Mutex<T>> is common in Rust programs that use shared-state parallelism. Stepping back from the technical details, observe that Rust has entirely avoided the problem of data races that plagues multithreaded programming in other languages. Of course, this good news is restricted to safe Rust—unsafe code (Item 16) and FFI boundaries in particular (Item 34)