Python threading Barrier Class

The threading.Barrier class in Python's threading module provides a way to synchronize a fixed number of threads. This is useful for scenarios where multiple threads need to wait for each other to reach a certain point before continuing execution.

Table of Contents

  1. Introduction
  2. threading.Barrier Class Syntax
  3. Examples
    • Basic Usage
    • Using a Barrier with Multiple Threads
    • Barrier with Timeout
  4. Real-World Use Case
  5. Conclusion

Introduction

The threading.Barrier class is used to synchronize a fixed number of threads, known as parties. Each thread waits at the barrier until all parties have reached it. Once all parties have reached the barrier, they are released simultaneously.

threading.Barrier Class Syntax

Here is how you create and use a barrier with the threading.Barrier class:

import threading

barrier = threading.Barrier(parties, action=None, timeout=None)

Parameters:

  • parties: The number of threads that must call wait() before they are all released.
  • action: Optional. A callable that is called by one of the threads when they are released.
  • timeout: Optional. The default timeout value for all wait() calls.

Methods:

  • wait(timeout=None): Wait for all parties to call this method. When they all have, they are all released simultaneously. An optional timeout can be provided to override the default.
  • reset(): Reset the barrier to the initial state.
  • abort(): Put the barrier into a broken state.
  • broken: A property that is True if the barrier is in a broken state.

Examples

Basic Usage

Create and use a barrier to synchronize threads.

Example

import threading
import time

def worker(barrier, worker_id):
    print(f"Worker {worker_id} waiting at barrier")
    barrier.wait()
    print(f"Worker {worker_id} passed barrier")

barrier = threading.Barrier(3)
threads = [threading.Thread(target=worker, args=(barrier, i)) for i in range(3)]

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

Output:

Worker 0 waiting at barrier
Worker 1 waiting at barrier
Worker 2 waiting at barrier
Worker 0 passed barrier
Worker 1 passed barrier
Worker 2 passed barrier

Using a Barrier with Multiple Threads

Synchronize more threads using a barrier.

Example

import threading
import time

def worker(barrier, worker_id):
    time.sleep(worker_id)
    print(f"Worker {worker_id} waiting at barrier")
    barrier.wait()
    print(f"Worker {worker_id} passed barrier")

barrier = threading.Barrier(5)
threads = [threading.Thread(target=worker, args=(barrier, i)) for i in range(5)]

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

Output:

Worker 0 waiting at barrier
Worker 1 waiting at barrier
Worker 2 waiting at barrier
Worker 3 waiting at barrier
Worker 4 waiting at barrier
Worker 0 passed barrier
Worker 1 passed barrier
Worker 2 passed barrier
Worker 3 passed barrier
Worker 4 passed barrier

Barrier with Timeout

Use a barrier with a timeout to handle situations where threads might not reach the barrier in time.

Example

import threading
import time

def worker(barrier, worker_id, delay):
    time.sleep(delay)
    print(f"Worker {worker_id} waiting at barrier")
    try:
        barrier.wait(timeout=3)
        print(f"Worker {worker_id} passed barrier")
    except threading.BrokenBarrierError:
        print(f"Worker {worker_id} barrier broken")

barrier = threading.Barrier(3)
threads = [threading.Thread(target=worker, args=(barrier, i, i * 2)) for i in range(3)]

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

Output:

Worker 0 waiting at barrier
Worker 1 waiting at barrier
Worker 2 waiting at barrier
Worker 0 passed barrier
Worker 1 barrier broken
Worker 2 barrier broken

Real-World Use Case

Synchronizing Phases in a Multi-Step Process

Use a barrier to synchronize threads at different phases of a multi-step process.

Example

import threading
import time

def phase1(barrier):
    print("Phase 1 started")
    time.sleep(2)
    print("Phase 1 waiting at barrier")
    barrier.wait()
    print("Phase 1 completed")

def phase2(barrier):
    print("Phase 2 started")
    time.sleep(1)
    print("Phase 2 waiting at barrier")
    barrier.wait()
    print("Phase 2 completed")

barrier = threading.Barrier(2)
thread1 = threading.Thread(target=phase1, args=(barrier,))
thread2 = threading.Thread(target=phase2, args=(barrier,))

thread1.start()
thread2.start()

thread1.join()
thread2.join()

Output:

Phase 1 started
Phase 2 started
Phase 2 waiting at barrier
Phase 1 waiting at barrier
Phase 2 completed
Phase 1 completed

Conclusion

The threading.Barrier class is used for synchronizing threads in multithreaded programs. It allows threads to wait for each other at a specific point, ensuring that they proceed together. This is particularly useful in scenarios where threads need to coordinate their actions at certain stages of execution. Proper usage can significantly enhance the reliability and control of your concurrent applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare