Synchronization

a barrier and a latch

Imagine a group hike where everyone must regroup at each trail marker before the next leg: faster walkers arrive and wait, and only when the last person shows up does the whole group set off together. A barrier is that regrouping point for threads: a synchronization point that no thread passes until a fixed number of threads have all reached it, at which moment they are all released together.

A barrier is created for a known number of participants, say N. Each thread, on reaching the barrier, calls wait (for example pthread_barrier_wait). The first N-1 threads to arrive block; when the Nth arrives, the barrier 'trips' and all N threads are released to continue. The classic use is phase-by-phase parallel computation: split work across N threads, have them all finish phase 1 and meet at a barrier, then all start phase 2 together — guaranteeing no thread reads phase-2 data before every thread has produced its phase-1 result. A latch (such as a countdown latch) is the close cousin meant for one-shot coordination: it is initialised to a count, threads can wait on it, and other events count it down; when the count hits zero the latch opens and stays open, releasing all waiters. The crucial difference is reusability and direction: a barrier is symmetric (the same N workers that wait are the ones that arrive) and is typically reusable for many phases, whereas a latch is one-way and single-use — once it opens it cannot close — and the threads that count it down need not be the ones that wait on it (one thread can wait for several others to finish setup).

These show up wherever you need 'everyone reach this point before anyone goes on' (a barrier) or 'wait until these N things have happened' (a latch). A barrier suits iterative simulations and parallel-for loops with synchronized phases; a latch suits start-up gating ('hold all workers until the server is initialised') and join-like waiting ('the main thread waits for 5 setup tasks to report done'). The common error with a barrier is a mismatch between the count it was created with and the number of threads that actually call wait — too few and they all block forever; too many and the extras either error or roll into the next phase unexpectedly.

pthread_barrier_init(&b, NULL, N); each worker: compute_phase1(); pthread_barrier_wait(&b); compute_phase2();. No thread starts phase 2 until all N have finished phase 1. A latch instead: main waits on a count of 5 that each setup task decrements once.

A barrier releases all N together at a meeting point; a latch opens once when its count hits zero.

A barrier blocks until exactly the configured number of threads call wait — if even one fewer arrives (a thread died early or took a different path), the rest hang forever. A latch is single-use: it counts down and opens once and cannot be reset, so use a barrier, not a latch, when you need to synchronise repeatedly.

Also called
barrier synchronizationcountdown latchrendezvous同步屏障倒數閂