Synchronizing Threads in Python With Barriers (71/100 Days of Python)

Martin Mirakyan
3 min readMar 13, 2023

--

Day 71 of the “100 Days of Python” blog post series covering synchronization in threads with barriers

In a multithreaded application, it’s common for multiple threads to work together to accomplish a task. However, there are certain situations where a thread must wait for other threads to reach a certain point in the execution before proceeding. This is where thread barriers come into play.

A thread barrier is a synchronization mechanism that allows threads to wait for each other to reach a certain point in the execution before continuing. Thread barriers can be used to ensure that all threads have completed a certain task before moving on to the next one, or to coordinate the order of execution of multiple threads.

How Thread Barriers Work

Thread barriers work by blocking threads until a certain number of threads have reached the barrier. Once the required number of threads have reached the barrier, all the blocked threads are released and allowed to continue execution.

A thread barrier typically has a count, which specifies the number of threads that must reach the barrier before the threads are released. When a thread reaches the barrier, it calls the wait() method on the barrier object. If the required number of threads have not yet reached the barrier, the calling thread is blocked. Once the required number of threads have reached the barrier, all the blocked threads are released and allowed to continue execution.

Using Thread Barriers in Python

In Python, thread barriers are implemented using the threading.Barrier() class. The Barrier() class takes a single argument, which specifies the number of threads that must reach the barrier before they are released.

Here’s an example of how to use a thread barrier in Python:

import threading

# create a barrier that requires 2 threads to reach the barrier
barrier = threading.Barrier(2)

def worker():
print('Worker started')
# do some work
print('Worker reached the barrier')
# wait for other threads to reach the barrier
barrier.wait()
# continue work
print('Worker finished')

# create two threads
t1 = threading.Thread(target=worker)
t2 = threading.Thread(target=worker)

# start the threads
t1.start()
t2.start()

# wait for the threads to finish
t1.join()
t2.join()

In this example, we create a Barrier() object with a count of 2, which means that two threads must reach the barrier before they are released. We then define a worker() function, which does some work and then reaches the barrier by calling the wait() method on the barrier object. Once both threads have reached the barrier, they continue executing and finishing their work.

Note that thread barriers can only be used once. After all, threads have reached the barrier, the barrier is reset and can be used again.

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet