-
Notifications
You must be signed in to change notification settings - Fork 6
/
stream_lock.py
56 lines (44 loc) · 1.01 KB
/
stream_lock.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
"""
Using global variable for inter thread communication
multi thread example
"""
from time import sleep
import _thread
def core0_thread():
global lock
while True:
# try to acquire lock - wait if in use
lock.acquire()
print('C')
sleep(0.5)
print('O')
sleep(0.5)
print('R')
sleep(0.5)
print('E')
sleep(0.5)
print('0')
sleep(0.5)
# release lock
lock.release()
def core1_thread():
global lock
while True:
# try to acquire lock - wait if in use
lock.acquire()
print('c')
sleep(0.5)
print('o')
sleep(0.5)
print('r')
sleep(0.5)
print('e')
sleep(0.5)
print('1')
sleep(0.5)
# release lock
lock.release()
# create a global lock
lock = _thread.allocate_lock()
second_thread = _thread.start_new_thread(core1_thread, ())
core0_thread()