"""
This file shows a simple example of a producer-consumer pattern with a race condition.
In class we'll explore how we can both exacerbate and remove the race condition.
"""


import random
from threading import Thread

words = ("adieu","audio","bayou","eerie","queue")

wqueue = []
nqueue = []

def wordmaker(n):
    """Adds {n} words to a list"""
    global wqms
    for i in range(n):
        word = random.choice(words)
        wqueue.append(word)

def nummaker(n):
    """Adds {n} numbers to a list"""
    global nqms
    for i in range(n):
        num = random.randrange(100,1000)
        nqueue.append(num)

def packer(n):
    """Pairs up words and numbers from two lists and displays the result"""
    for i in range(n):
        pair = wqueue.pop(), nqueue.pop()
        print(pair)

# Make a thread to run each of the above functions
wworker = Thread(target=wordmaker, args=[1000])
nworker = Thread(target=nummaker, args=[1000])
pworker = Thread(target=packer, args=[1000])

# Make the list-filling threads daemons (no need to join them)
wworker.daemon = True
nworker.daemon = True

# Start all three threads
wworker.start()
nworker.start()
pworker.start()

# Wait for the packing thread to finish
pworker.join()