-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExclusiveQueue.py
43 lines (38 loc) · 904 Bytes
/
ExclusiveQueue.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
33
34
35
36
37
38
39
40
41
42
43
# Osama M. Afifi [email protected] - 20/Nov/2014
#
# Exclusive Concurrency Pattern
#
# We have two queues Leader & Followers and want both doSomething() together where each leader must have an exclusive corresponding follower
# We don't want followers or leaders queue up in this solution
# This code is deadlock free if you can see anything else feel free to tell me
mutex = Semaphore(1)
randevu = Semaphore(0)
followerQueue = Semaphore(0)
leaderQueue = Semaphore(0)
followers = 0
leader = 0
def doSomething():
pass
def leaderStatment():
mutex.wait()
if followers>0:
followers--
followerQueue.signal()
else:
leaders++
mutex.signal()
leaderQueue.wait()
doSomething()
randevu.wait()
mutex.signal()
def followerStatment():
mutex.wait()
if leaders>0:
leaders--
leaderQueue.signal()
else:
followers++
mutex.signal()
followerQueue.wait()
doSomething()
randevu.signal()