Skip to the content.

Synchronization Primitives - Barrier

This class provides a simple synchronization primitive for use by a fixed number of threads that need to wait for each other.

Solution

import threading

THRESHOLD = 2

def worker(b: threading.Barrier):
    try:
        b.wait()
    except threading.BrokenBarrierError:
        # to handle


b = threading.Barrier(THRESHOLD)
t1 = threading.Thread(target=worker, name='t1', args=(b,))
t2 = threading.Thread(target=worker, name='t2', args=(b,))
t1.start()
t2.start()

b.abort()

t1.join()
t2.join()

References