-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
52 lines (34 loc) · 1.06 KB
/
example.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
from threading import Thread
from rwmutex import RWLock
from time import sleep
lock = RWLock()
shared_resource = ""
def do_writes():
global lock
global shared_resource
print("writer thread waiting for write lock")
with lock.write:
print("writer thread received write lock")
for i in range(10):
print(f"writing {i}")
shared_resource += f"{i} "
sleep(1)
print("writer thread will yield write lock")
def do_read(id):
global lock
global shared_resource
print(f"reader thread {id} waiting for read lock")
with lock.read:
print(f"reader thread {id} received read lock")
print(f"reader thread {id} found '{shared_resource}'")
print(f"reader thread {id} will yield read lock")
threads = []
writer_thread = Thread(target=do_writes, daemon=True)
writer_thread.start()
threads.append(writer_thread)
for i in range(5):
reader_thread = Thread(target=do_read, args=[i])
reader_thread.start()
threads.append(reader_thread)
for t in threads:
t.join()