-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFinite_Producer_Consumer.py
32 lines (28 loc) · 1014 Bytes
/
Finite_Producer_Consumer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Osama M. Afifi [email protected] - 24/Nov/2014
#
# Producer Consumer Concurrency Pattern
#
# We have a producer which produces some kind of object and adds it to a buffer and a consumer which consume this object
# Accessing the buffer should be exclusive
# This technique is used in even handling where producers wait for events and consumers are called event handlers
# This version offers a finite buffer with Max size specified in BUFFER_LIMIT
# This code is deadlock free if you can see anything else feel free to tell me
mutex = Sempahore(1) # The buffer mutex.
items = Semaphore(0) # Block when buffer is empty (no items).
space = Semaphore(BUFFER_LIMIT) # Available space sem with max number of BUFFER_LIMIT items in the buffer,
def Producer:
while True:
event = waitForEvent()
space.wait()
mutex.wait()
buffer.add(event)
mutex.signal
items.signal()
def Consumer:
while True:
items.wait()
mutex.wait()
event = buffer.get()
mutex.signal()
space.signal()
event.process();