-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode15.py
More file actions
42 lines (28 loc) · 946 Bytes
/
code15.py
File metadata and controls
42 lines (28 loc) · 946 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
34
35
36
37
38
39
40
41
42
# code15.py
import threading
class FirstProcess(threading.Thread):
def __init__(self, lock_a, lock_b):
self.lock_a = lock_a
self.lock_b = lock_b
def run(self):
with self.lock_a:
# Acquire the first lock
with self.lock_b:
# Acquire the second lock for another concurrent task
class SecondProcess(threading.Thread):
def __init__(self, lock_a, lock_b):
self.lock_a = lock_a
self.lock_b = lock_b
def run(self):
with self.lock_b:
# Acquire the first lock
# Notice that this thread require the lock_b, that
# could be taken fot other thread previously
with self.lock_a:
# Acquire the second lock for another concurrent task
lock_a = threading.Lock()
lock_b = threading.Lock()
t1 = FirstProcess(lock_a, lock_b)
t2 = SecondProcess(lock_a, lock_b)
t1.start()
t2.start()