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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
#!/usr/bin/env python
#
# A port of the semaphore example from TQt.
import sys
# Check if thread support was enabled.
try:
from python_tqt.qt import TQThread
except:
print "Thread support not enabled"
sys.exit(1)
from python_tqt.qt import *
# The semaphore instances.
yellowSem = None
greenSem = None
class YellowThread(TQThread):
def __init__(self,o):
TQThread.__init__(self)
self.receiver = o
self.stopped = 0
self.mutex = TQMutex()
def run(self):
global yellowSem, greenSem
for i in range(20):
yellowSem += 1
event = TQCustomEvent(12345)
event.setData(TQString("Yellow!"))
TQThread.postEvent(self.receiver,event)
self.msleep(200);
greenSem -= 1
self.mutex.lock()
if self.stopped:
self.stopped = 0
self.mutex.unlock()
break
self.mutex.unlock()
yellowSem += 1
event = TQCustomEvent(12346)
event.setData(TQString("Yellow!"))
TQThread.postEvent(self.receiver,event)
greenSem -= 1
def stop(self):
self.mutex.lock()
self.stopped = 1
self.mutex.unlock()
class GreenThread(TQThread):
def __init__(self,o):
TQThread.__init__(self)
self.receiver = o
self.stopped = 0
self.mutex = TQMutex()
def run(self):
global yellowSem, greenSem
for i in range(20):
greenSem += 1
event = TQCustomEvent(12345)
event.setData(TQString("Green!"))
TQThread.postEvent(self.receiver,event)
self.msleep(200)
yellowSem -= 1
self.mutex.lock()
if self.stopped:
self.stopped = 0
self.mutex.unlock()
break
self.mutex.unlock()
greenSem += 1
event = TQCustomEvent(12346)
event.setData(TQString("Green!"))
TQThread.postEvent(self.receiver,event)
self.msleep(10)
yellowSem -= 1
def stop(self):
self.mutex.lock()
self.stopped = 1
self.mutex.unlock()
class SemaphoreExample(TQWidget):
def __init__(self):
TQWidget.__init__(self)
self.yellowThread = YellowThread(self)
self.greenThread = GreenThread(self)
global yellowSem, greenSem
yellowSem = TQSemaphore(1)
greenSem = TQSemaphore(1)
self.button = TQPushButton("&Ignition!",self)
self.connect(self.button,SIGNAL("clicked()"),self.startExample)
self.mlineedit = TQMultiLineEdit(self)
self.label = TQLabel(self)
vbox = TQVBoxLayout(self,5)
vbox.addWidget(self.button)
vbox.addWidget(self.mlineedit)
vbox.addWidget(self.label)
def __del__(self):
stopYellow = self.yellowThread.running()
stopGreen = self.greenThread.running()
if stopYellow:
self.yellowThread.stop()
if self.greenThread.running():
self.greenThread.stop()
if stopYellow:
self.yellowThread.wait()
if stopGreen:
self.greenThread.wait()
global yellowSem, greenSem
yellowSem = None
greenSem = None
def startExample(self):
if self.yellowThread.running() or self.greenThread.running():
TQMessageBox.information(self,"Sorry",
"The threads have not completed yet, and must finish before "
"they can be started again.")
return
self.mlineedit.clear()
global yellowSem
while yellowSem.available() < yellowSem.total():
yellowSem -= 1
yellowSem += 1
self.yellowThread.start()
self.greenThread.start()
def customEvent(self,event):
if event.type() == 12345:
s = event.data()
self.mlineedit.append(s)
if s.latin1() == "Green!":
self.label.setBackgroundColor(TQt.green)
else:
self.label.setBackgroundColor(TQt.yellow)
self.label.setText(s)
del s
elif event.type() == 12346:
s = event.data()
TQMessageBox.information(self,s.latin1() + " - Finished",
"The thread creating the \"" + s.latin1() +
"\" events has finished.")
del s
else:
print "Unknown custom event type:", event.type()
app = TQApplication(sys.argv)
se = SemaphoreExample()
app.setMainWidget(se)
se.show()
sys.exit(app.exec_loop())
|