-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads.py
executable file
·82 lines (58 loc) · 1.68 KB
/
threads.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
import threading
import time
import random
exitFlag = False
gyro_value = 0
compass_value = 0
gyroLock = threading.Lock()
compassLock = threading.Lock()
class gyroThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print("Starting gyroThread")
while (True):
if exitFlag:
exit()
time.sleep(1)
gyroLock.acquire()
# time.sleep(1)
global gyro_value
gyro_value = random.randint(0, 5)
print("setting gyro value to {}".format(gyro_value))
gyroLock.release()
class compassThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print("Starting compassThread")
while (True):
if exitFlag:
exit()
time.sleep(1)
compassLock.acquire()
# time.sleep(1)
global compass_value
compass_value = random.randint(0, 5)
print("setting compass value to {}".format(compass_value))
compassLock.release()
gt = gyroThread()
ct = compassThread()
gt.start()
ct.start()
try:
while threading.active_count() > 0:
print("Main thread: reading values")
compassLock.acquire()
c = compass_value
compassLock.release()
gyroLock.acquire()
g = gyro_value
gyroLock.release()
# time.sleep(3)
print("Main thread: gyro value={}, compass value={}".format(g, c))
time.sleep(0.1)
except KeyboardInterrupt:
print("Ctrl-c received! Sending kill to threads...")
exitFlag = True