April 23, 2024

Condition Variables - A cool synchronization technique!

I was a bit taken aback to come to know about condition-variables this late in my journey. Condition variables allow you to wait and notify other threads when a certain event occurs. I understood it best through an example. Following is a code sample for Producer-Consumer problem using mutexes. producer-consumer-just-mutex.py from threading import Thread, Lock import time, random mutex = Lock() items = [] def timer(t): while t: time.sleep(1) print(f"Time: {t}") t -= 1 def producer(): while True: with mutex: print("Acquired lock in producer") items....