知识卡片
分离读写丢失更新
每次原子 load 和 store 都安全,并不表示整段读改写原子。单值计数可用 fetch_add,多字段可用锁或其他状态所有者。测试应控制读取和写入之间的交错、验证最终不变量,而非碰运气多运行几遍。关联 [[CAS校验更新前提]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 11. Testing 原子单步不保证复合更新 · 查看原书依据
Figure 11-2 shows two async tasks that are getting a number from a data store and increasing the number by one. Because the second task gets the data before the first task has updated the data store, both tasks increase from 10, which results in the data being 11 when it should be 12. Throughout this book, we have protected against this with mutexes or
参考来源
- 位置:《Async Rust Unleashing the Power of Fearless Concurrency (Maxwell Flitton, Caroline Morton)》第11章《Chapter 11. Testing》,原子单步不保证复合更新(来源项目 book-source,extracted/Async Rust Unleashing the Power of Fearless Concurrency (Maxwell Flitton, Caroline Morton)/_epub-src/OEBPS/ch11.html#id72)
- 结论依据:原书用两个任务读到 10 并都写 11 说明逻辑竞态,然后改变运行时与让出点。
- 原始内容:Figure 11-2 shows two async tasks that are getting a number from a data store and increasing the number by one. Because the second task gets the data before the first task has updated the data store, both tasks increase from 10, which results in the data being 11 when it should be 12. Throughout this book, we have protected against this with mutexes or