Preventing Starvation
Strong vs Weak Semaphores
Weak semaphore: When you signal, some waiting thread wakes up. No guarantee which one. A thread could theoretically wait forever.
Strong semaphore: Uses FIFO queue. When you signal, the thread that waited longest wakes up. Once you join the queue, a bounded number of threads can go ahead of you.
Most modern systems provide strong semaphores (FIFO). But do not assume - check your platform documentation.
The Turnstile Pattern
Morris solved starvation using two turnstiles creating a waiting room. The key insight: batch threads into groups. Current batch finishes before new arrivals can proceed.
Phase 1: New threads enter waiting room, first turnstile open.
Phase 2: First turnstile closes, second opens. Waiting threads proceed to critical section.
New arrivals must wait for next cycle. Nobody can cut in line indefinitely.
Practical Prevention Strategies
Fair locks: Use locks with FIFO acquisition order (ReentrantLock(true) in Java).
Priority aging: Gradually increase priority of waiting threads. Eventually even low-priority threads become high-priority.
Turnstile for readers-writers: When a writer arrives, close a turnstile. Existing readers finish, writer proceeds, then readers continue.