The Rendezvous Pattern
The Puzzle
Given two threads, guarantee that a1 happens before b2 AND b1 happens before a2. Both threads must complete their first statement before either does their second.
Why Signal Before Wait?
If both threads wait first, deadlock! Thread A waits for B to signal, B waits for A to signal, nobody signals. By signaling first, you announce your arrival before checking if the other arrived.
Think Of It Like Meeting A Friend
You agree to meet at a coffee shop. When you arrive, you text "I'm here" (signal), then wait for their "I'm here" text (wait). If you both do this, you will find each other regardless of who arrives first.
The Barrier Generalization
Rendezvous is a two-thread barrier. Barriers generalize this to N threads: everyone must arrive before anyone proceeds. Useful for parallel algorithms where phases must synchronize.