The Rendezvous Pattern
Why Mutual Waiting?
Basic signaling is one-way: A signals B. But sometimes both threads need to wait for each other. Thread A does part 1, Thread B does part 1. Neither can do part 2 until both finish part 1. They must synchronize at a meeting point.
The Airport Gate Analogy
Two friends flying from different cities meet at a connecting airport. Alice lands at 2pm, Bob at 3pm. Alice waits at the gate. When Bob arrives, they both proceed to the next flight together. Neither boards until both have arrived.
The Solution
Use two semaphores, both initialized to 0. Each thread signals its own "arrived" semaphore, then waits on the other. The order is critical: signal first, then wait. Reversing this causes deadlock.
Why Signal Before Wait?
If both threads wait before signaling, both block forever. Neither can signal because both are stuck waiting. This is deadlock. By signaling first, at least one thread makes progress even if it then blocks.