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
- Is a kind of: Synchronization
- Often confused with: Latch
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¶
- In this library: Is total += n safe on two threads?
- In a sibling library: Go: A buffered channel as a semaphore ↗
- In the books: Learn Concurrent Programming with Go, James Cutajar — ch. 6, 'Synchronizing with waitgroups and barriers'
- In the books: Hands-On Concurrency with Rust, Brian L. Troutwine — ch. 5, 'Locks – Mutex, Condvar, Barriers and RWLock'
- In the books: The Art of Multiprocessor Programming, Maurice Herlihy, Nir Shavit — ch. 17, 'Barriers'
- In the books: Concurrency with Modern C++, Rainer Grimm — ch. 6, 'The Future: C++20/23' → 'Latches and Barriers'
- In the books: Pro Asynchronous Programming with .NET, Richard Blewett, Andrew Clymer — ch. 4, 'Basic Thread Safety' → 'Barrier: Rendezvous-Based Synchronization'
- In the books: Data Parallel C++, James Reinders, Ben Ashbaugh, James Brodman, Michael Kinsner, John Pennycook, Xinmin Tian — ch. 9, 'Communication and Synchronization' → 'Using Work-Group Barriers and Local Memory'
- Notes: std::barrier - C++ ↗
- Notes: Synchronisation Patterns - C++ ↗