← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.e.1.b.3.b · 生成模型:未记录

跨线程的两种标记

理解等级:中级

T: Send 表示值可以转移到另一线程,T: Sync 表示共享引用 &T 可以安全地被 Send。Rc 的计数更新未经线程同步,因此没有这两种标记。RefCell 的运行时借用状态也不能供多个线程共享,Mutex 则可通过锁实现满足约束的共享访问。选择类型时先判断是在转移值,还是同时共享对该值的访问。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 无畏并发 Extensible Concurrency with the Send and Sync Traits · 查看原书依据

      The Sync marker trait indicates that it is safe for the type implementing Sync to be referenced from multiple threads. In other words, any type T is Sync if &T (an immutable reference to T) is Send, meaning the reference can be sent safely to another thread. Similar to Send, primitive types are Sync, and types composed entirely of types that are Sync are also Sync.

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第16章 无畏并发(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c16.xhtml`,块 0188) - 结论依据:原文直接给出 Sync 与 &T: Send 的等价判据,并与前节 Send 的所有权语义相衔接。 - 原始内容:The Sync marker trait indicates that it is safe for the type implementing Sync to be referenced from multiple threads. In other words, any type T is Sync if &T (an immutable reference to T) is Send, meaning the reference can be sent safely to another thread. Similar to Send, primitive types are Sync, and types composed entirely of types that are Sync are also Sync.