<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">from threading import Thread, Lock, Condition, Semaphore
import time
import random

class EventTracker:
    def __init__(self):
	self.hungrycustomer_lock = Lock()
        self.hungrycustomer = Condition(self.hungrycustomer_lock)
	self.numburgers = 0
    
    def customerenter(self):
        with self.hungrycustomer_lock:
            while self.numburgers == 0 :
                self.hungrycustomer.wait()
            #check if indeed there is a burger
            assert(self.numburgers &gt; 0)
            self.numburgers -= 1

    def produceburger(self):
        with self.hungrycustomer_lock:
            self.numburgers += 1
            self.hungrycustomer.notify()
                
class Customer(Thread):

    def __init__(self):
        Thread.__init__(self)

    def run(self):
        global burger_house
        for i in range(20):
            #hungry
            #enter
            burger_house.customerenter()
            #eat the burger
            sleep_time = random.randint(1, 10000)
	    time.sleep(sleep_time/10000.0)
            #leave
	

class Cook(Thread):

    def __init__(self):
        Thread.__init__(self)

    def run(self):
        global burger_house 
	for i in range(100):
	    #make a burger
	    burger_house.produceburger()
            #rest
	    sleep_time = random.randint(1, 10000)
	    time.sleep(sleep_time/100000.0)

burger_house = EventTracker()
		
cook = Cook()
		
for i in range(5):
    customer = Customer()
    customer.start()

cook.start()

	
# vim:expandtab:tabstop=8:shiftwidth=4:softtabstop=4
</pre></body></html>