Barrier (start together)
flowchart TD A["Barrier(3)"] --> B["thread 1 calls wait() -- parks"] B --> C["thread 2 calls wait() -- parks"] C --> D["thread 3 calls wait()"] D --> E["the party is complete"] E --> F["all three released together"] F --> G["each gets a distinct index; exactly one gets 0"] G --> H["that one can do the once-per-round work"] E --> I["the barrier RESETS for the next round"] J["a thread that never arrives"] --> K["everyone waits forever -- use a timeout"]
What is a Barrier?
Section titled “What is a Barrier?”A Barrier makes a group of threads wait until all have reached a point.
Use cases:
- start multiple workers at the same time
- multi-step simulations
Example
Section titled “Example”import threading
import time
barrier = threading.Barrier(3)
def worker(i: int) -> None:
time.sleep(0.2 * i)
print("worker", i, "ready")
barrier.wait()
print("worker", i, "started")
threads = [threading.Thread(target=worker, args=(i,)) for i in range(3)]
for t in threads:
t.start()
for t in threads:
t.join()- If a thread fails and never reaches the barrier, others can block.
- You can set a timeout in
barrier.wait(timeout=...).
Check yourself
Section titled “Check yourself”-
`Barrier(3)`. Two threads have called `wait()`. What are they doing?
Verified: with 2 of 3 arrived, neither had passed. A barrier releases nobody until the party count is reached.
pch.quizShowAnswer
B — Both blocked, waiting for the third — Verified: with 2 of 3 arrived, neither had passed. A barrier releases nobody until the party count is reached.
-
What does `barrier.wait()` return?
Each participant gets a different index. Checking for 0 is the standard way to nominate one thread for once-per-round work without needing another lock.
pch.quizShowAnswer
C — A distinct index per thread, exactly one of which is 0 — Each participant gets a different index. Checking for 0 is the standard way to nominate one thread for once-per-round work without needing another lock.
-
After all threads pass, what state is the barrier in?
A barrier is reusable by design, which is what makes it suited to looping phases — every worker finishes round N before any starts round N+1.
pch.quizShowAnswer
B — Reset, ready for the next round — A barrier is reusable by design, which is what makes it suited to looping phases — every worker finishes round N before any starts round N+1.
-
One participant crashes before reaching the barrier. What happens to the others?
There is no default timeout. Pass one to `wait()` and handle `BrokenBarrierError`, which is what the remaining threads receive once the barrier is broken.
pch.quizShowAnswer
B — They wait forever unless a timeout was given — There is no default timeout. Pass one to `wait()` and handle `BrokenBarrierError`, which is what the remaining threads receive once the barrier is broken.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Basic Barrier
Section titled “Exercise 1 – Basic Barrier”Exercise 2 – Barrier Parties Count
Section titled “Exercise 2 – Barrier Parties Count”Exercise 3 – Abort a Barrier
Section titled “Exercise 3 – Abort a Barrier”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading