// Release the lock to prevent a deadlock drop(guard); println!("thread {} is ready", i); // Blocks the current thread until all threads have rendezvoused here. my_barrier.wait(); println!("thread {} is done", i) }); }
// A barrier will block `n`-1 threads which call [`wait()`] and then wake // up all threads at once when the `n`th thread calls [`wait()`]. barrier.wait();
Barrier::wait coordinates n parties; you fix n when you create the barrier. Each wait blocks until the nth caller arrives, then everyone wakes and continues—like a gate every thread must reach before anyone moves on.
Compared with join-all, this version has real overhead when thread counts grow; internally it’s condvar plus mutex. That pairing is its own topic—see Rust Condvar.