Little Book of Semaphores 中的饥饿释放互斥量

Starvation Free Mutex In Little Book of Semaphores

背景:

Allen B. Downey 的 The Little Book of Semaphores 谈到了防止线程饥饿所需的假设。

他指出调度程序需要保证以下内容:

Property 2: if a thread is ready to run, then the time it waits until it runs is bounded.

并且弱信号量保证:

Property 3: if there are threads waiting on a semaphore when a thread executes signal, then one of the waiting threads has to be woken.

但是,他指出,即使具有这些属性,当 运行 3 个或更多线程(线程 A、B、C)时,以下代码也会导致饥饿:

while True: 
    mutex.wait()
    # critical section 
    mutex.signal()

论据是,如果A先执行,然后唤醒B,A可以在B释放之前再次等待互斥体。此时,A可以再次被唤醒,重新获取互斥锁,并与B重复这个循环。C将被饿死。

问题:

难道 属性2 不能保证 C 必须在某个有限的时间内被调度程序唤醒吗?如果是这样,那么线程 C 就不会饿死。就算weak semaphore不保证Thread C一定会被唤醒,调度器不应该运行吗?

我稍微考虑了一下,意识到 属性 2 可以保证处于 RUNNABLE 状态的线程将在有限的时间内被调度。

书中的论点指出线程 C 永远不会进入 RUNNABLE 状态,因此 属性 2 和 3 不能保证没有饥饿。