-
Notifications
You must be signed in to change notification settings - Fork 6
/
thread_global.py
54 lines (40 loc) · 1.09 KB
/
thread_global.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
"""
Using global variable for inter thread communication
multi thread example
"""
from time import sleep
import _thread
def core0_thread():
global run_core_1
counter = 0
while True:
# print next 5 even numbers
for loop in range(5):
print(counter)
counter += 2
sleep(1)
# signal core 1 to run
run_core_1 = True
# wait for core 1 to finish
print("core 0 waiting")
while run_core_1:
pass
def core1_thread():
global run_core_1
counter = 1
while True:
# wait for core 0 to signal start
print("core 1 waiting")
while not run_core_1:
pass
# print next 3 odd numbers
for loop in range(3):
print(counter)
counter += 2
sleep(0.5)
# signal core 0 code finished
run_core_1 = False
# Global variable to send signals between threads
run_core_1 = False
second_thread = _thread.start_new_thread(core1_thread, ())
core0_thread()