... continued

Continuation of discussion on condition variables.

We'll cover the following...

We create a condition variable as follows:

Creating a condition variable

cond_var = Condition()

The two important methods of a condition variable are:

  • wait() - invoked to make a thread sleep and give up resources

  • notify() - invoked by a thread when a condition becomes true and the invoking threads want to inform the waiting thread or threads to proceed

A condition variable is always associated with a lock. The lock can be either reentrant or a plain vanilla lock. The associated lock must be acquired before a thread can invoke wait()or notify() on the condition variable.

Incorrect way of using condition variables

cond_var = Condition()
cond_var.wait()  # throws an error
Press + to interact
from threading import Condition
cond_var = Condition()
cond_var.wait() # throws an error

We can create a lock ourselves and pass it to the condition variable's constructor. If no lock object is passed then a lock is created underneath the hood by the condition variable. In the examples below we demonstrate all the different ways of invoking condition variables.

We can create the condition variable as follows:

Creating a condition variable by passing

a custom lock

lock = Lock()
cond_var =
...