实现没有饥饿问题的锁。
实现算法:
Morris's solution
。
It uses two turnstiles to create two waiting rooms before the critical section.The mechanism works in two phases.
During the first phase, the first turnstile is open and the second is closed, so threads accumulate in the second room.During the second phase, the first turnstile is closed, so no new threads can enter, and the second is open, so the existing threads can get to the critical section.
成员变量
成员变量的初始化
算法框架
Morris 算法
mutex
用于保护临界区变量 room1
, 在t1.wait()
部分实现了等待线程到达room1
的过程,