-
Notifications
You must be signed in to change notification settings - Fork 1
/
prodcons.py
55 lines (45 loc) · 1.24 KB
/
prodcons.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
44
45
46
47
48
49
50
51
52
53
54
55
from monitor import Monitor
from threading import Thread
from time import sleep
class Buffer(Monitor):
def __init__(self, id):
self.size = 5
self.buffer = []
super().__init__(id, data=self.buffer)
class Producer(Thread):
def __init__(self, id):
super().__init__()
self.buf = Buffer(id)
def run(self):
sleep(1)
for i in range(0, 100):
self.buf.request_cs()
while len(self.buf.get()) >= self.buf.size:
self.buf.wait()
data = self.buf.get()
print('put', i)
data.append(i)
self.buf.set(data)
self.buf.leave_cs()
self.buf.pulse_all()
class Consumer(Thread):
def __init__(self, id):
super().__init__()
self.b = Buffer(id)
def run(self):
sleep(1)
while True:
self.b.request_cs()
while len(self.b.get()) <= 0:
self.b.wait()
data = self.b.get()
print('\tget', data.pop())
self.b.set(data)
# sleep(0.01)
self.b.leave_cs()
self.b.pulse_all()
Producer(0).start()
Consumer(1).start()
Producer(2).start()
Consumer(3).start()
Consumer(4).start()