-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconflict.py
67 lines (55 loc) · 1.42 KB
/
conflict.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
56
57
58
59
60
61
62
63
64
65
66
67
from threading import Thread
import time
import random
q = list()
a = 100
def produttore(name):
global a
i = 1
while True:
if a % 2 == 0:
a = a + 2
else:
a = a + 1
i = i + 1
print(f"p({i}): {a}")
# global q
# print(f"thread {name} started")
# working = True
# for k in range(100):
# time.sleep(0.5)
# x = random.randint(0,100)
# q.insert(0,x) # estendi la lista
# print(f"From producer: {x}, q = {q}")
# # scrivi x nel primo elemento
# if x == 100:
# working = False
# break
# if working:
# q.insert(0,100)
def consumatore(name):
global a
i = 1
while True:
a = a + 1
print(f"c({i}): {a}")
i = i + 1
# global q
# working = True
# print(f"thread {name} started")
# while working:
# print(f"Consumer: coda = {q}")
# if len(q) > 0:
# x = q.pop()
# if x == 100:
# working = False
# print(f"Consumer: popped {x}")
# time.sleep(0.5)
# print(f"Consumer ended")
if __name__ == "__main__":
thread1 = Thread(target=produttore, args=("Produttore",))
thread2 = Thread(target=consumatore, args=("Consumatore",))
thread1.start()
thread2.start()
thread1.join()
thread2.join()