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