]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: test.cpp | |
3 | // Purpose: wxWindows thread sample | |
4 | // Author: Julian Smart(minimal)/Guilhem Lavaux(thread test) | |
5 | // Modified by: | |
6 | // Created: 06/16/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart, Markus Holzem, Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | /* | |
13 | TODO: | |
14 | ||
15 | 1. show how SetPriority() works. | |
16 | 2. use worker threads to update progress controls instead of writing | |
17 | messages - it will be more visual | |
18 | */ | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "test.cpp" | |
22 | #pragma interface "test.cpp" | |
23 | #endif | |
24 | ||
25 | // For compilers that support precompilation, includes "wx/wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
32 | #ifndef WX_PRECOMP | |
33 | #include "wx/wx.h" | |
34 | #endif | |
35 | ||
36 | #if !wxUSE_THREADS | |
37 | #error "This sample requires thread support!" | |
38 | #endif // wxUSE_THREADS | |
39 | ||
40 | #include "wx/thread.h" | |
41 | #include "wx/dynarray.h" | |
42 | #include "wx/time.h" | |
43 | ||
44 | // Define a new application type | |
45 | class MyApp : public wxApp | |
46 | { | |
47 | public: | |
48 | bool OnInit(); | |
49 | }; | |
50 | ||
51 | class MyThread; | |
52 | WX_DEFINE_ARRAY(wxThread *, wxArrayThread); | |
53 | ||
54 | // Define a new frame type | |
55 | class MyFrame: public wxFrame | |
56 | { | |
57 | public: | |
58 | // ctor | |
59 | MyFrame(wxFrame *frame, const wxString& title, int x, int y, int w, int h); | |
60 | ||
61 | // operations | |
62 | void WriteText(const wxString& text) { m_txtctrl->WriteText(text); } | |
63 | ||
64 | // callbacks | |
65 | void OnQuit(wxCommandEvent& event); | |
66 | void OnAbout(wxCommandEvent& event); | |
67 | void OnClear(wxCommandEvent& event); | |
68 | ||
69 | void OnStartThread(wxCommandEvent& event); | |
70 | void OnStartThreads(wxCommandEvent& event); | |
71 | void OnStopThread(wxCommandEvent& event); | |
72 | void OnPauseThread(wxCommandEvent& event); | |
73 | void OnResumeThread(wxCommandEvent& event); | |
74 | ||
75 | void OnIdle(wxIdleEvent &event); | |
76 | ||
77 | // called by dying thread _in_that_thread_context_ | |
78 | void OnThreadExit(wxThread *thread); | |
79 | ||
80 | private: | |
81 | // helper function - creates a new thread (but doesn't run it) | |
82 | MyThread *CreateThread(); | |
83 | ||
84 | // crit section protects access to all of the arrays below | |
85 | wxCriticalSection m_critsect; | |
86 | ||
87 | // all the threads currently alive - as soon as the thread terminates, it's | |
88 | // removed from the array | |
89 | wxArrayThread m_threads; | |
90 | ||
91 | // just some place to put our messages in | |
92 | wxTextCtrl *m_txtctrl; | |
93 | ||
94 | // remember the number of running threads and total number of threads | |
95 | size_t m_nRunning, m_nCount; | |
96 | ||
97 | DECLARE_EVENT_TABLE() | |
98 | }; | |
99 | ||
100 | class MyThread : public wxThread | |
101 | { | |
102 | public: | |
103 | MyThread(MyFrame *frame); | |
104 | ||
105 | // thread execution starts here | |
106 | virtual void *Entry(); | |
107 | ||
108 | // called when the thread exits - whether it terminates normally or is | |
109 | // stopped with Delete() (but not when it is Kill()ed!) | |
110 | virtual void OnExit(); | |
111 | ||
112 | // write something to the text control | |
113 | void WriteText(const wxString& text); | |
114 | ||
115 | public: | |
116 | size_t m_count; | |
117 | MyFrame *m_frame; | |
118 | }; | |
119 | ||
120 | MyThread::MyThread(MyFrame *frame) | |
121 | : wxThread() | |
122 | { | |
123 | m_count = 0; | |
124 | m_frame = frame; | |
125 | } | |
126 | ||
127 | void MyThread::WriteText(const wxString& text) | |
128 | { | |
129 | wxString msg; | |
130 | ||
131 | // before doing any GUI calls we must ensure that this thread is the only | |
132 | // one doing it! | |
133 | wxMutexGuiLocker guiLocker; | |
134 | msg << wxTime().FormatTime() << ": " << text; | |
135 | ||
136 | m_frame->WriteText(msg); | |
137 | } | |
138 | ||
139 | void MyThread::OnExit() | |
140 | { | |
141 | m_frame->OnThreadExit(this); | |
142 | } | |
143 | ||
144 | void *MyThread::Entry() | |
145 | { | |
146 | wxString text; | |
147 | ||
148 | text.Printf("Thread 0x%x started.\n", GetID()); | |
149 | WriteText(text); | |
150 | ||
151 | for ( m_count = 0; m_count < 10; m_count++ ) | |
152 | { | |
153 | // check if we were asked to exit | |
154 | if ( TestDestroy() ) | |
155 | break; | |
156 | ||
157 | text.Printf("[%u] Thread 0x%x here.\n", m_count, GetID()); | |
158 | WriteText(text); | |
159 | ||
160 | // wxSleep() can't be called from non-GUI thread! | |
161 | wxThread::Sleep(1000); | |
162 | } | |
163 | ||
164 | text.Printf("Thread 0x%x finished.\n", GetID()); | |
165 | WriteText(text); | |
166 | ||
167 | return NULL; | |
168 | } | |
169 | ||
170 | // ID for the menu commands | |
171 | enum | |
172 | { | |
173 | TEST_QUIT = 1, | |
174 | TEST_TEXT = 101, | |
175 | TEST_ABOUT, | |
176 | TEST_CLEAR, | |
177 | TEST_START_THREAD = 201, | |
178 | TEST_START_THREADS, | |
179 | TEST_STOP_THREAD, | |
180 | TEST_PAUSE_THREAD, | |
181 | TEST_RESUME_THREAD | |
182 | }; | |
183 | ||
184 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
185 | EVT_MENU(TEST_QUIT, MyFrame::OnQuit) | |
186 | EVT_MENU(TEST_ABOUT, MyFrame::OnAbout) | |
187 | EVT_MENU(TEST_CLEAR, MyFrame::OnClear) | |
188 | EVT_MENU(TEST_START_THREAD, MyFrame::OnStartThread) | |
189 | EVT_MENU(TEST_START_THREADS, MyFrame::OnStartThreads) | |
190 | EVT_MENU(TEST_STOP_THREAD, MyFrame::OnStopThread) | |
191 | EVT_MENU(TEST_PAUSE_THREAD, MyFrame::OnPauseThread) | |
192 | EVT_MENU(TEST_RESUME_THREAD, MyFrame::OnResumeThread) | |
193 | ||
194 | EVT_IDLE(MyFrame::OnIdle) | |
195 | END_EVENT_TABLE() | |
196 | ||
197 | // Create a new application object | |
198 | IMPLEMENT_APP (MyApp) | |
199 | ||
200 | // `Main program' equivalent, creating windows and returning main app frame | |
201 | bool MyApp::OnInit() | |
202 | { | |
203 | // Create the main frame window | |
204 | MyFrame *frame = new MyFrame((wxFrame *)NULL, "wxWindows threads sample", | |
205 | 50, 50, 450, 340); | |
206 | ||
207 | // Make a menubar | |
208 | wxMenu *file_menu = new wxMenu; | |
209 | ||
210 | file_menu->Append(TEST_CLEAR, "&Clear log"); | |
211 | file_menu->AppendSeparator(); | |
212 | file_menu->Append(TEST_ABOUT, "&About"); | |
213 | file_menu->AppendSeparator(); | |
214 | file_menu->Append(TEST_QUIT, "E&xit"); | |
215 | wxMenuBar *menu_bar = new wxMenuBar; | |
216 | menu_bar->Append(file_menu, "&File"); | |
217 | ||
218 | wxMenu *thread_menu = new wxMenu; | |
219 | thread_menu->Append(TEST_START_THREAD, "&Start a new thread"); | |
220 | thread_menu->Append(TEST_START_THREADS, "Start &many threads at once"); | |
221 | thread_menu->Append(TEST_STOP_THREAD, "S&top a running thread"); | |
222 | thread_menu->AppendSeparator(); | |
223 | thread_menu->Append(TEST_PAUSE_THREAD, "&Pause a running thread"); | |
224 | thread_menu->Append(TEST_RESUME_THREAD, "&Resume suspended thread"); | |
225 | menu_bar->Append(thread_menu, "&Thread"); | |
226 | frame->SetMenuBar(menu_bar); | |
227 | ||
228 | // Show the frame | |
229 | frame->Show(TRUE); | |
230 | ||
231 | SetTopWindow(frame); | |
232 | ||
233 | return TRUE; | |
234 | } | |
235 | ||
236 | // My frame constructor | |
237 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, | |
238 | int x, int y, int w, int h) | |
239 | : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)) | |
240 | { | |
241 | m_nRunning = m_nCount = 0; | |
242 | ||
243 | CreateStatusBar(2); | |
244 | ||
245 | m_txtctrl = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(0, 0), | |
246 | wxTE_MULTILINE | wxTE_READONLY); | |
247 | ||
248 | } | |
249 | ||
250 | MyThread *MyFrame::CreateThread() | |
251 | { | |
252 | MyThread *thread = new MyThread(this); | |
253 | ||
254 | if ( thread->Create() != wxTHREAD_NO_ERROR ) | |
255 | { | |
256 | wxLogError("Can't create thread!"); | |
257 | } | |
258 | ||
259 | wxCriticalSectionLocker enter(m_critsect); | |
260 | m_threads.Add(thread); | |
261 | ||
262 | return thread; | |
263 | } | |
264 | ||
265 | void MyFrame::OnStartThreads(wxCommandEvent& WXUNUSED(event) ) | |
266 | { | |
267 | static wxString s_str; | |
268 | s_str = wxGetTextFromUser("How many threads to start: ", | |
269 | "wxThread sample", | |
270 | s_str, this); | |
271 | if ( s_str.IsEmpty() ) | |
272 | return; | |
273 | ||
274 | size_t count, n; | |
275 | sscanf(s_str, "%u", &count); | |
276 | if ( count == 0 ) | |
277 | return; | |
278 | ||
279 | wxArrayThread threads; | |
280 | ||
281 | // first create them all... | |
282 | for ( n = 0; n < count; n++ ) | |
283 | { | |
284 | threads.Add(CreateThread()); | |
285 | } | |
286 | ||
287 | wxString msg; | |
288 | msg.Printf("%d new threads created.", count); | |
289 | SetStatusText(msg, 1); | |
290 | ||
291 | // ...and then start them | |
292 | for ( n = 0; n < count; n++ ) | |
293 | { | |
294 | threads[n]->Run(); | |
295 | } | |
296 | } | |
297 | ||
298 | void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) ) | |
299 | { | |
300 | MyThread *thread = CreateThread(); | |
301 | ||
302 | if ( thread->Run() != wxTHREAD_NO_ERROR ) | |
303 | { | |
304 | wxLogError("Can't start thread!"); | |
305 | } | |
306 | ||
307 | SetStatusText("New thread started.", 1); | |
308 | } | |
309 | ||
310 | void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) ) | |
311 | { | |
312 | // stop the last thread | |
313 | if ( m_threads.IsEmpty() ) | |
314 | { | |
315 | wxLogError("No thread to stop!"); | |
316 | } | |
317 | else | |
318 | { | |
319 | m_critsect.Enter(); | |
320 | ||
321 | wxThread *thread = m_threads.Last(); | |
322 | ||
323 | // it's important to leave critical section before calling Delete() | |
324 | // because delete will (implicitly) call OnThreadExit() which also tries | |
325 | // to enter the same crit section - would dead lock. | |
326 | m_critsect.Leave(); | |
327 | ||
328 | thread->Delete(); | |
329 | ||
330 | SetStatusText("Thread stopped.", 1); | |
331 | } | |
332 | } | |
333 | ||
334 | void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) ) | |
335 | { | |
336 | wxCriticalSectionLocker enter(m_critsect); | |
337 | ||
338 | // resume first suspended thread | |
339 | size_t n = 0, count = m_threads.Count(); | |
340 | while ( n < count && !m_threads[n]->IsPaused() ) | |
341 | n++; | |
342 | ||
343 | if ( n == count ) | |
344 | { | |
345 | wxLogError("No thread to resume!"); | |
346 | } | |
347 | else | |
348 | { | |
349 | m_threads[n]->Resume(); | |
350 | ||
351 | SetStatusText("Thread resumed.", 1); | |
352 | } | |
353 | } | |
354 | ||
355 | void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) ) | |
356 | { | |
357 | wxCriticalSectionLocker enter(m_critsect); | |
358 | ||
359 | // pause last running thread | |
360 | int n = m_threads.Count() - 1; | |
361 | while ( n >= 0 && !m_threads[n]->IsRunning() ) | |
362 | n--; | |
363 | ||
364 | if ( n < 0 ) | |
365 | { | |
366 | wxLogError("No thread to pause!"); | |
367 | } | |
368 | else | |
369 | { | |
370 | m_threads[n]->Pause(); | |
371 | ||
372 | SetStatusText("Thread paused.", 1); | |
373 | } | |
374 | } | |
375 | ||
376 | // set the frame title indicating the current number of threads | |
377 | void MyFrame::OnIdle(wxIdleEvent &event) | |
378 | { | |
379 | // update the counts of running/total threads | |
380 | size_t nRunning = 0, | |
381 | nCount = m_threads.Count(); | |
382 | for ( size_t n = 0; n < nCount; n++ ) | |
383 | { | |
384 | if ( m_threads[n]->IsRunning() ) | |
385 | nRunning++; | |
386 | } | |
387 | ||
388 | if ( nCount != m_nCount || nRunning != m_nRunning ) | |
389 | { | |
390 | m_nRunning = nRunning; | |
391 | m_nCount = nCount; | |
392 | ||
393 | wxLogStatus(this, "%u threads total, %u running.", nCount, nRunning); | |
394 | } | |
395 | //else: avoid flicker - don't print anything | |
396 | } | |
397 | ||
398 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
399 | { | |
400 | size_t count = m_threads.Count(); | |
401 | for ( size_t i = 0; i < count; i++ ) | |
402 | { | |
403 | m_threads[i]->Delete(); | |
404 | } | |
405 | ||
406 | Close(TRUE); | |
407 | } | |
408 | ||
409 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
410 | { | |
411 | wxMessageDialog dialog(this, "wxWindows multithreaded application sample\n" | |
412 | "(c) 1998 Julian Smart, Guilhem Lavaux\n" | |
413 | "(c) 1999 Vadim Zeitlin", | |
414 | "About wxThread sample", | |
415 | wxOK | wxICON_INFORMATION); | |
416 | ||
417 | dialog.ShowModal(); | |
418 | } | |
419 | ||
420 | void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event)) | |
421 | { | |
422 | m_txtctrl->Clear(); | |
423 | } | |
424 | ||
425 | void MyFrame::OnThreadExit(wxThread *thread) | |
426 | { | |
427 | wxCriticalSectionLocker enter(m_critsect); | |
428 | ||
429 | m_threads.Remove(thread); | |
430 | } |