知识卡片

结构图卡 · 6074.e.1.b.7.a

拆锁可能破坏跨表不变量

理解等级:中级

把玩家表与游戏表操作拆成互不重叠的持锁段,能避开示例死锁,却允许“登记用户→封禁移除→加入游戏”交错,产生游戏中的用户不在玩家表。同步方案应围绕跨结构不变量设计;需一起变化的状态可合并为单锁保护的GameState。

结构图:

classDiagram
class GameState {
  players: HashMap~String_Player~
  games: HashMap~GameId_Game~
}
class GameServer {
  state: Mutex~GameState~
}
GameServer *-- GameState : 同一Mutex保护整体状态

图为原书合并后的结构,泛型下划线分隔键值。单锁使相关操作可在同一临界区执行,但仍需避免持锁调用不受控代码。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 3. Concepts Deadlocks · 查看原书依据

      data consistency problem: the players and games data structures can get out of sync with each other, given an execution sequence like the one shown in Table 3-2. Table 3-2. State inconsistency sequence Thread 1 Thread 2 Enters add_and_join("Alice") and adds Alice to the players data structure (then releases the players lock). Enters ban_player("Alice") and r

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Deadlocks》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u104-ch03-039)。 - 结论依据:原文在“data consistency problem”附近的定义、例子或代码支撑本卡核心说明:把玩家表与游戏表操作拆成互不重叠的持锁段,能避开示例死锁,却允许“登记用户→封禁移除→加入游戏”交错,产生游戏中的用户不在玩家表。边界按已读前提解释;示例未执行。 - 原始内容:data consistency problem: the players and games data structures can get out of sync with each other, given an execution sequence like the one shown in Table 3-2. Table 3-2. State inconsistency sequence Thread 1 Thread 2 Enters add_and_join("Alice") and adds Alice to the players data structure (then releases the players lock). Enters ban_player("Alice") and r