-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
49 lines (36 loc) · 884 Bytes
/
test.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
# encoding: utf-8
'''
@author: ZiqiLiu
@file: test.py
@time: 2018/2/19 下午10:42
@desc:
'''
from RWLOCK import RWLock as rwlock1
from RWLOCK2 import RWLock as rwlock2
import time
import threading
lock = rwlock2()
def read(tid):
lock.r_acquire()
print('thread %d is reading...' % tid)
time.sleep(2)
print('thread %d finish reading')
lock.r_release()
def write(tid):
lock.w_acquire()
print('thread %d is writing...' % tid)
time.sleep(2)
print('thread %d finish writing' % tid)
lock.w_release()
if __name__ == '__main__':
jobs = ['r', 'r', 'w', 'r', 'r', 'w']
ts = []
for i, j in enumerate(jobs):
if j == 'r':
t = threading.Thread(target=read, args=(i,))
else:
t = threading.Thread(target=write, args=(i,))
t.start()
ts.append(t)
for t in ts:
t.join()