-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock.py
More file actions
33 lines (24 loc) · 715 Bytes
/
lock.py
File metadata and controls
33 lines (24 loc) · 715 Bytes
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
import multiprocessing
import time
def deposit(balance, lock):
for i in range(100):
time.sleep(0.01)
lock.acquire()
balance.value = balance.value + 1
lock.release()
def withdraw(balance, lock):
for i in range(100):
time.sleep(0.01)
lock.acquire()
balance.value = balance.value - 1
lock.release()
if __name__ == '__main__':
b = multiprocessing.Value('i', 200)
l = multiprocessing.Lock()
d = multiprocessing.Process(target=deposit, args=(b,l))
w = multiprocessing.Process(target=withdraw, args=(b,l))
d.start()
w.start()
d.join()
w.join()
print("balance: ", b.value)