← Lessons

quiz vs the machine

Gold1420

Concurrency

Barrier Synchronization

A meeting point where every thread waits until all have arrived before any moves on.

4 min read · core · beat Gold to climb

Everyone waits for everyone

A barrier synchronizes a group of threads at a common point. Each thread that reaches the barrier blocks until the whole group has arrived, then all are released together.

How it counts

  • The barrier knows the expected party size.
  • Each arriving thread increments an arrival count and waits.
  • When the count equals the party size, the barrier wakes every waiter.

This is ideal for phased computation. In a simulation, all threads must finish updating phase one before any reads neighbors in phase two. The barrier guarantees no thread races ahead.

The hazard

If a thread crashes or never arrives, the others wait forever. Barriers therefore assume a fixed, known set of participants that all reliably reach the meeting point.

Key idea

A barrier makes every thread pause until the full group arrives, then releases them together, which keeps phased parallel work in lockstep but stalls forever if a participant goes missing.

Check yourself

Answer to earn rating on the learn ladder.

1. When does a barrier release the waiting threads?

2. What is the main hazard of a barrier?