Skip to content

Barrier

Category: Synchronization · Status: stub · Lessons: chapter 04, Waiting for each other (planned)

One line: A meeting point for a fixed number of tasks: each waits there until all of them have arrived, then all continue together.

Also called: CyclicBarrier, std::sync::Barrier, rendezvous.

How it connects

flowchart LR
  n_barrier["Barrier"]
  n_latch["Latch"]
  n_synchronization["Synchronization"]
  n_barrier ---|vs| n_latch
  n_barrier -->|is a| n_synchronization
  classDef center stroke-width:3px
  class n_barrier center
  classDef outside stroke-dasharray: 4 3
  class n_latch,n_synchronization outside

In each language

Rust std::sync::Barrier enables multiple threads to synchronize the beginning of some computation
Go None in sync; closing a channel releases every goroutine waiting to receive from it, because after close receives no longer block
Java CyclicBarrier can be reset and reused; if one waiter is interrupted or times out, the others leave with BrokenBarrierException
Python threading.Barrier: a timeout or abort breaks it, and waiters get BrokenBarrierError
C# Barrier is reused through several phases of an algorithm
The operating system POSIX pthread_barrier_wait returns PTHREAD_BARRIER_SERIAL_THREAD to one arbitrary thread and zero to the others

Where to read more