Javatpoint Logo
Javatpoint Logo

Barrier Objects in Python

A barrier object allows a group of threads to wait for each other before continuing execution. It can be useful for tasks that need to be performed in a specific order, or for tasks that need to be synchronized to avoid race conditions.

These are used to synchronize the execution of threads. When a thread calls the wait() method on a barrier object, it will block until the specified number of threads have also called wait() on the same barrier object. Once the specified number of threads have called wait(), all of the threads will be released and can continue executing.

Initializing a Barrier

Threading is used to initialize the barrier. Let's see the following syntax.

Syntax -

The number_of_threads is that barrier should wait upon. An action represents the callable to a called by the one of the threads when they are released. The timeout is the default timeout value if none is specified for the wait() method.

Example -

Output:

200
201
Exit

Following are some functions related to the barrier threading.

1. Checking the state of the barrier - The broker is used to check the state of the barrier. The syntax is given below.

2. parties - The number of threads required to pass the barrier.

3. Aborting a barrier - The abort() is used to abort the barrier. Put the barrier into a broken state. To prevent program execution from becoming deadlocked, it is often necessary to incorporate abort function calls when encountering barriers.

Syntax -

4. Resetting the barrier - By resetting the barrier to its default, empty state, any threads that are waiting on it will be notified through the BrokenBarrierError exception.

Syntax -

5. wait - When all the thread party to the barrier have called the wait() function, they all are released simultaneously. In other words, if you specify a timeout when calling the wait() method, that timeout will be used, even if you specified a different timeout in the constructor. This allows you to override the timeout value if necessary. In other words, the wait() method returns an integer that is unique to each thread. This integer can be used to identify which thread was the last to call the wait() method. If the call times out, the barrier is put into the broken state. This means that the barrier will no longer function properly. The wait() method may also raise a BrokenBarrierError exception if the barrier is broken or reset while a thread is waiting. A barrier is broken if it is reset or released while there are still threads waiting on it.

Syntax -

6. n_waiting - It shows number of threads currently waiting in the barrier.

Syntax

Let's understand the following example.

Example -

Output:

100
Parties =  3
n_waiting =  0
101
Parties =  3
n_waiting =  2
False
n_waiting after reset =  0
End






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA