知识卡片
互斥锁守卫自动释放
Mutex 把对普通共享状态的数据访问纳入互斥协议。lock 获取锁后返回 MutexGuard,持有可变守卫时可用解引用修改内层值,守卫的 Drop 自动释放锁。把守卫作用域限制在访问所需的范围,其他线程就能在该范围结束后继续。持有者恐慌可能使锁中毒,lock 的错误应结合数据是否可恢复处理。
相关卡片 [[跨线程共享可变数据]]、[[互斥访问仍会死锁]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 无畏并发 Shared-State Concurrency · 查看原书依据
As you might suspect, Mutex<T> is a smart pointer. More accurately, the call to lock returns a smart pointer called MutexGuard, wrapped in a LockResult that we handled with the call to unwrap.
参考来源
- 位置:《The Rust Programming Language(第2版)》第16章 无畏并发(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c16.xhtml`,块 0144)
- 结论依据:原文解释 lock 返回 LockResult 中的守卫、Deref 到内层数据及离开作用域的 Drop 解锁。
- 原始内容:As you might suspect, Mutex<T> is a smart pointer. More accurately, the call to lock returns a smart pointer called MutexGuard, wrapped in a LockResult that we handled with the call to unwrap.