]>
Commit | Line | Data |
---|---|---|
82052aff | 1 | ///////////////////////////////////////////////////////////////////////////// |
c4f02b1f | 2 | // Name: thread.cpp |
be5a51fb | 3 | // Purpose: wxWidgets thread sample |
ffc45b67 | 4 | // Author: Guilhem Lavaux, Vadim Zeitlin |
82052aff GL |
5 | // Modified by: |
6 | // Created: 06/16/98 | |
7 | // RCS-ID: $Id$ | |
be5a51fb | 8 | // Copyright: (c) 1998-2002 wxWidgets team |
3222fde2 | 9 | // Licence: wxWindows license |
82052aff GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
82052aff GL |
12 | // For compilers that support precompilation, includes "wx/wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
3222fde2 | 16 | #pragma hdrstop |
82052aff GL |
17 | #endif |
18 | ||
19 | #ifndef WX_PRECOMP | |
3222fde2 | 20 | #include "wx/wx.h" |
82052aff GL |
21 | #endif |
22 | ||
3222fde2 VZ |
23 | #if !wxUSE_THREADS |
24 | #error "This sample requires thread support!" | |
25 | #endif // wxUSE_THREADS | |
26 | ||
82052aff GL |
27 | #include "wx/thread.h" |
28 | #include "wx/dynarray.h" | |
e4f3eb42 | 29 | #include "wx/numdlg.h" |
82052aff | 30 | |
b9de1315 VZ |
31 | #include "wx/progdlg.h" |
32 | ||
df315913 JS |
33 | #include "../sample.xpm" |
34 | ||
ffc45b67 | 35 | // define this to use wxExecute in the exec tests, otherwise just use system |
cd5e9298 | 36 | #define USE_EXECUTE |
ffc45b67 | 37 | |
a4b59324 VZ |
38 | #ifdef USE_EXECUTE |
39 | #define EXEC(cmd) wxExecute((cmd), wxEXEC_SYNC) | |
40 | #else | |
41 | #define EXEC(cmd) system(cmd) | |
42 | #endif | |
b8b9762a | 43 | |
1bd3e1ef | 44 | class MyThread; |
38d6b957 | 45 | WX_DEFINE_ARRAY_PTR(wxThread *, wxArrayThread); |
1bd3e1ef | 46 | |
82052aff | 47 | // Define a new application type |
bf1852e1 | 48 | class MyApp : public wxApp |
82052aff | 49 | { |
98f026a6 | 50 | public: |
ffc45b67 | 51 | MyApp(); |
925e9792 | 52 | virtual ~MyApp(){}; |
ffc45b67 | 53 | |
98f026a6 VZ |
54 | virtual bool OnInit(); |
55 | ||
abbcf16d VZ |
56 | // critical section protects access to all of the fields below |
57 | wxCriticalSection m_critsect; | |
58 | ||
98f026a6 VZ |
59 | // all the threads currently alive - as soon as the thread terminates, it's |
60 | // removed from the array | |
61 | wxArrayThread m_threads; | |
62 | ||
963ac8fb VZ |
63 | // semaphore used to wait for the threads to exit, see MyFrame::OnQuit() |
64 | wxSemaphore m_semAllDone; | |
ffc45b67 | 65 | |
abbcf16d VZ |
66 | // indicates that we're shutting down and all threads should exit |
67 | bool m_shuttingDown; | |
82052aff GL |
68 | }; |
69 | ||
1bd3e1ef | 70 | // Create a new application object |
98f026a6 | 71 | IMPLEMENT_APP(MyApp) |
82052aff GL |
72 | |
73 | // Define a new frame type | |
74 | class MyFrame: public wxFrame | |
75 | { | |
a6b0bd49 VZ |
76 | public: |
77 | // ctor | |
bf1852e1 | 78 | MyFrame(wxFrame *frame, const wxString& title, int x, int y, int w, int h); |
4e5bbd40 | 79 | virtual ~MyFrame(); |
3222fde2 | 80 | |
3a105cbc VZ |
81 | // this function is MT-safe, i.e. it can be called from worker threads |
82 | // safely without any additional locking | |
83 | void LogThreadMessage(const wxString& text) | |
84 | { | |
85 | wxCriticalSectionLocker lock(m_csMessages); | |
86 | m_messages.push_back(text); | |
87 | ||
88 | // as we effectively log the messages from the idle event handler, | |
89 | // ensure it's going to be called now that we have some messages to log | |
90 | wxWakeUpIdle(); | |
91 | } | |
a6b0bd49 | 92 | |
b9de1315 VZ |
93 | // accessors for MyWorkerThread (called in its context!) |
94 | bool Cancelled(); | |
95 | ||
3a105cbc VZ |
96 | private: |
97 | // event handlers | |
82052aff | 98 | void OnQuit(wxCommandEvent& event); |
bee503b0 | 99 | void OnClear(wxCommandEvent& event); |
a6b0bd49 | 100 | |
82052aff | 101 | void OnStartThread(wxCommandEvent& event); |
7c3d7e2d | 102 | void OnStartThreads(wxCommandEvent& event); |
82052aff GL |
103 | void OnStopThread(wxCommandEvent& event); |
104 | void OnPauseThread(wxCommandEvent& event); | |
a6b0bd49 | 105 | void OnResumeThread(wxCommandEvent& event); |
b9de1315 | 106 | |
ce6d2511 | 107 | void OnStartWorker(wxCommandEvent& event); |
d48b06bd | 108 | void OnWorkerEvent(wxThreadEvent& event); |
b9de1315 | 109 | void OnUpdateWorker(wxUpdateUIEvent& event); |
a6b0bd49 | 110 | |
a4b59324 VZ |
111 | void OnExecMain(wxCommandEvent& event); |
112 | void OnExecThread(wxCommandEvent& event); | |
113 | ||
114 | void OnShowCPUs(wxCommandEvent& event); | |
115 | void OnAbout(wxCommandEvent& event); | |
116 | ||
3222fde2 | 117 | void OnIdle(wxIdleEvent &event); |
3222fde2 | 118 | |
7c3d7e2d VZ |
119 | // helper function - creates a new thread (but doesn't run it) |
120 | MyThread *CreateThread(); | |
88ac883a | 121 | |
3a105cbc VZ |
122 | // update display in our status bar: called during idle handling |
123 | void UpdateThreadStatus(); | |
124 | ||
125 | // log the messages queued by LogThreadMessage() | |
126 | void DoLogThreadMessages(); | |
127 | ||
128 | ||
bf1852e1 VZ |
129 | // just some place to put our messages in |
130 | wxTextCtrl *m_txtctrl; | |
3222fde2 | 131 | |
3a105cbc VZ |
132 | // the array of pending messages to be displayed and the critical section |
133 | // protecting it | |
134 | wxArrayString m_messages; | |
135 | wxCriticalSection m_csMessages; | |
136 | ||
7c3d7e2d | 137 | // remember the number of running threads and total number of threads |
3a105cbc VZ |
138 | size_t m_nRunning, |
139 | m_nCount; | |
7c3d7e2d | 140 | |
b9de1315 VZ |
141 | // the progress dialog which we show while worker thread is running |
142 | wxProgressDialog *m_dlgProgress; | |
143 | ||
144 | // was the worker thread cancelled by user? | |
145 | bool m_cancelled; | |
146 | ||
147 | // protects m_cancelled | |
148 | wxCriticalSection m_critsectWork; | |
149 | ||
a6b0bd49 | 150 | DECLARE_EVENT_TABLE() |
82052aff GL |
151 | }; |
152 | ||
ce6d2511 RR |
153 | // ID for the menu commands |
154 | enum | |
155 | { | |
91b07357 JS |
156 | THREAD_QUIT = wxID_EXIT, |
157 | THREAD_ABOUT = wxID_ABOUT, | |
a4b59324 VZ |
158 | THREAD_TEXT = 101, |
159 | THREAD_CLEAR, | |
160 | THREAD_START_THREAD = 201, | |
161 | THREAD_START_THREADS, | |
162 | THREAD_STOP_THREAD, | |
163 | THREAD_PAUSE_THREAD, | |
164 | THREAD_RESUME_THREAD, | |
165 | THREAD_START_WORKER, | |
166 | ||
167 | THREAD_EXEC_MAIN, | |
168 | THREAD_EXEC_THREAD, | |
169 | ||
170 | THREAD_SHOWCPUS, | |
a4b59324 | 171 | |
d48b06bd | 172 | WORKER_EVENT = wxID_HIGHEST+1 // this one gets sent from the worker thread |
ce6d2511 RR |
173 | }; |
174 | ||
a4b59324 | 175 | // ---------------------------------------------------------------------------- |
ce6d2511 | 176 | // GUI thread |
a4b59324 | 177 | // ---------------------------------------------------------------------------- |
ce6d2511 | 178 | |
bee503b0 | 179 | class MyThread : public wxThread |
82052aff | 180 | { |
a6b0bd49 | 181 | public: |
82052aff | 182 | MyThread(MyFrame *frame); |
abbcf16d | 183 | virtual ~MyThread(); |
3222fde2 VZ |
184 | |
185 | // thread execution starts here | |
186 | virtual void *Entry(); | |
187 | ||
abbcf16d VZ |
188 | // write something to the text control in the main frame |
189 | void WriteText(const wxString& text) | |
190 | { | |
191 | m_frame->LogThreadMessage(text); | |
192 | } | |
a6b0bd49 VZ |
193 | |
194 | public: | |
b143cf70 | 195 | unsigned m_count; |
82052aff GL |
196 | MyFrame *m_frame; |
197 | }; | |
198 | ||
199 | MyThread::MyThread(MyFrame *frame) | |
a6b0bd49 | 200 | : wxThread() |
82052aff | 201 | { |
a6b0bd49 VZ |
202 | m_count = 0; |
203 | m_frame = frame; | |
82052aff GL |
204 | } |
205 | ||
abbcf16d | 206 | MyThread::~MyThread() |
bee503b0 | 207 | { |
1bd3e1ef GL |
208 | wxCriticalSectionLocker locker(wxGetApp().m_critsect); |
209 | ||
ffc45b67 VZ |
210 | wxArrayThread& threads = wxGetApp().m_threads; |
211 | threads.Remove(this); | |
212 | ||
213 | if ( threads.IsEmpty() ) | |
214 | { | |
215 | // signal the main thread that there are no more threads left if it is | |
216 | // waiting for us | |
abbcf16d | 217 | if ( wxGetApp().m_shuttingDown ) |
ffc45b67 | 218 | { |
abbcf16d | 219 | wxGetApp().m_shuttingDown = false; |
ffc45b67 | 220 | |
963ac8fb | 221 | wxGetApp().m_semAllDone.Post(); |
ffc45b67 VZ |
222 | } |
223 | } | |
3222fde2 VZ |
224 | } |
225 | ||
82052aff GL |
226 | void *MyThread::Entry() |
227 | { | |
a6b0bd49 | 228 | wxString text; |
3222fde2 | 229 | |
12a3f227 | 230 | text.Printf(wxT("Thread 0x%lx started (priority = %u).\n"), |
b568d04f | 231 | GetId(), GetPriority()); |
3222fde2 | 232 | WriteText(text); |
2286341c | 233 | // wxLogMessage(text); -- test wxLog thread safeness |
3222fde2 | 234 | |
bee503b0 | 235 | for ( m_count = 0; m_count < 10; m_count++ ) |
3222fde2 | 236 | { |
abbcf16d VZ |
237 | // check if the application is shutting down: in this case all threads |
238 | // should stop a.s.a.p. | |
239 | { | |
240 | wxCriticalSectionLocker locker(wxGetApp().m_critsect); | |
241 | if ( wxGetApp().m_shuttingDown ) | |
242 | return NULL; | |
243 | } | |
244 | ||
245 | // check if just this thread was asked to exit | |
3222fde2 VZ |
246 | if ( TestDestroy() ) |
247 | break; | |
248 | ||
12a3f227 | 249 | text.Printf(wxT("[%u] Thread 0x%lx here.\n"), m_count, GetId()); |
3222fde2 | 250 | WriteText(text); |
a6b0bd49 | 251 | |
bf1852e1 VZ |
252 | // wxSleep() can't be called from non-GUI thread! |
253 | wxThread::Sleep(1000); | |
a6b0bd49 | 254 | } |
3222fde2 | 255 | |
12a3f227 | 256 | text.Printf(wxT("Thread 0x%lx finished.\n"), GetId()); |
3222fde2 | 257 | WriteText(text); |
2286341c | 258 | // wxLogMessage(text); -- test wxLog thread safeness |
3222fde2 | 259 | |
a6b0bd49 | 260 | return NULL; |
82052aff GL |
261 | } |
262 | ||
a4b59324 | 263 | // ---------------------------------------------------------------------------- |
ce6d2511 | 264 | // worker thread |
a4b59324 | 265 | // ---------------------------------------------------------------------------- |
ce6d2511 RR |
266 | |
267 | class MyWorkerThread : public wxThread | |
3222fde2 | 268 | { |
ce6d2511 RR |
269 | public: |
270 | MyWorkerThread(MyFrame *frame); | |
271 | ||
272 | // thread execution starts here | |
273 | virtual void *Entry(); | |
274 | ||
275 | // called when the thread exits - whether it terminates normally or is | |
276 | // stopped with Delete() (but not when it is Kill()ed!) | |
277 | virtual void OnExit(); | |
278 | ||
279 | public: | |
280 | MyFrame *m_frame; | |
b143cf70 | 281 | unsigned m_count; |
3222fde2 | 282 | }; |
82052aff | 283 | |
ce6d2511 RR |
284 | MyWorkerThread::MyWorkerThread(MyFrame *frame) |
285 | : wxThread() | |
286 | { | |
287 | m_frame = frame; | |
288 | m_count = 0; | |
289 | } | |
290 | ||
291 | void MyWorkerThread::OnExit() | |
292 | { | |
293 | } | |
294 | ||
74d60f66 | 295 | #define TEST_YIELD_RACE_CONDITION 0 |
d48b06bd | 296 | |
ce6d2511 RR |
297 | void *MyWorkerThread::Entry() |
298 | { | |
d48b06bd FM |
299 | #if TEST_YIELD_RACE_CONDITION |
300 | if ( TestDestroy() ) | |
301 | return NULL; | |
302 | ||
74d60f66 | 303 | wxThreadEvent event( wxEVT_COMMAND_THREAD, WORKER_EVENT ); |
d48b06bd FM |
304 | |
305 | event.SetInt( 50 ); | |
306 | wxQueueEvent( m_frame, new wxThreadEvent(event) ); | |
307 | ||
308 | event.SetInt(-1); | |
309 | wxQueueEvent( m_frame, new wxThreadEvent(event) ); | |
310 | #else | |
b9de1315 | 311 | for ( m_count = 0; !m_frame->Cancelled() && (m_count < 100); m_count++ ) |
ce6d2511 RR |
312 | { |
313 | // check if we were asked to exit | |
314 | if ( TestDestroy() ) | |
315 | break; | |
b9de1315 | 316 | |
b9de1315 | 317 | // create any type of command event here |
74d60f66 | 318 | wxThreadEvent event( wxEVT_COMMAND_THREAD, WORKER_EVENT ); |
b9de1315 | 319 | event.SetInt( m_count ); |
b9de1315 | 320 | |
07f5b19a | 321 | // send in a thread-safe way |
d48b06bd | 322 | wxQueueEvent( m_frame, new wxThreadEvent(event) ); |
b9de1315 | 323 | |
3a105cbc | 324 | wxMilliSleep(200); |
ce6d2511 RR |
325 | } |
326 | ||
74d60f66 | 327 | wxThreadEvent event( wxEVT_COMMAND_THREAD, WORKER_EVENT ); |
b9de1315 | 328 | event.SetInt(-1); // that's all |
d48b06bd FM |
329 | wxQueueEvent( m_frame, new wxThreadEvent(event) ); |
330 | #endif | |
b9de1315 | 331 | |
ce6d2511 RR |
332 | return NULL; |
333 | } | |
334 | ||
a4b59324 VZ |
335 | // ---------------------------------------------------------------------------- |
336 | // a thread which simply calls wxExecute | |
337 | // ---------------------------------------------------------------------------- | |
338 | ||
339 | class MyExecThread : public wxThread | |
340 | { | |
341 | public: | |
342 | MyExecThread(const wxChar *command) : wxThread(wxTHREAD_JOINABLE), | |
343 | m_command(command) | |
344 | { | |
345 | Create(); | |
346 | } | |
347 | ||
348 | virtual ExitCode Entry() | |
349 | { | |
cd431efc | 350 | return wxUIntToPtr(EXEC(m_command)); |
a4b59324 VZ |
351 | } |
352 | ||
353 | private: | |
354 | wxString m_command; | |
355 | }; | |
356 | ||
357 | // ---------------------------------------------------------------------------- | |
358 | // implementation | |
359 | // ---------------------------------------------------------------------------- | |
ce6d2511 | 360 | |
82052aff | 361 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
a4b59324 VZ |
362 | EVT_MENU(THREAD_QUIT, MyFrame::OnQuit) |
363 | EVT_MENU(THREAD_CLEAR, MyFrame::OnClear) | |
364 | EVT_MENU(THREAD_START_THREAD, MyFrame::OnStartThread) | |
365 | EVT_MENU(THREAD_START_THREADS, MyFrame::OnStartThreads) | |
366 | EVT_MENU(THREAD_STOP_THREAD, MyFrame::OnStopThread) | |
367 | EVT_MENU(THREAD_PAUSE_THREAD, MyFrame::OnPauseThread) | |
368 | EVT_MENU(THREAD_RESUME_THREAD, MyFrame::OnResumeThread) | |
369 | ||
370 | EVT_MENU(THREAD_EXEC_MAIN, MyFrame::OnExecMain) | |
371 | EVT_MENU(THREAD_EXEC_THREAD, MyFrame::OnExecThread) | |
372 | ||
373 | EVT_MENU(THREAD_SHOWCPUS, MyFrame::OnShowCPUs) | |
374 | EVT_MENU(THREAD_ABOUT, MyFrame::OnAbout) | |
375 | ||
376 | EVT_UPDATE_UI(THREAD_START_WORKER, MyFrame::OnUpdateWorker) | |
377 | EVT_MENU(THREAD_START_WORKER, MyFrame::OnStartWorker) | |
d48b06bd FM |
378 | |
379 | EVT_THREAD(WORKER_EVENT, MyFrame::OnWorkerEvent) | |
a6b0bd49 | 380 | |
3222fde2 | 381 | EVT_IDLE(MyFrame::OnIdle) |
82052aff GL |
382 | END_EVENT_TABLE() |
383 | ||
ffc45b67 | 384 | MyApp::MyApp() |
ffc45b67 | 385 | { |
abbcf16d | 386 | m_shuttingDown = false; |
ffc45b67 VZ |
387 | } |
388 | ||
82052aff | 389 | // `Main program' equivalent, creating windows and returning main app frame |
3222fde2 | 390 | bool MyApp::OnInit() |
82052aff | 391 | { |
45e6e6f8 VZ |
392 | if ( !wxApp::OnInit() ) |
393 | return false; | |
394 | ||
a4b59324 VZ |
395 | // uncomment this to get some debugging messages from the trace code |
396 | // on the console (or just set WXTRACE env variable to include "thread") | |
397 | //wxLog::AddTraceMask("thread"); | |
b8b9762a | 398 | |
a6b0bd49 | 399 | // Create the main frame window |
be5a51fb | 400 | MyFrame *frame = new MyFrame((wxFrame *)NULL, _T("wxWidgets threads sample"), |
bee503b0 | 401 | 50, 50, 450, 340); |
3222fde2 | 402 | |
a6b0bd49 | 403 | // Make a menubar |
a4b59324 VZ |
404 | wxMenuBar *menuBar = new wxMenuBar; |
405 | ||
406 | wxMenu *menuFile = new wxMenu; | |
ab1ca7b3 | 407 | menuFile->Append(THREAD_CLEAR, _T("&Clear log\tCtrl-L")); |
a4b59324 | 408 | menuFile->AppendSeparator(); |
ab1ca7b3 MB |
409 | menuFile->Append(THREAD_QUIT, _T("E&xit\tAlt-X")); |
410 | menuBar->Append(menuFile, _T("&File")); | |
a4b59324 VZ |
411 | |
412 | wxMenu *menuThread = new wxMenu; | |
ab1ca7b3 MB |
413 | menuThread->Append(THREAD_START_THREAD, _T("&Start a new thread\tCtrl-N")); |
414 | menuThread->Append(THREAD_START_THREADS, _T("Start &many threads at once")); | |
415 | menuThread->Append(THREAD_STOP_THREAD, _T("S&top a running thread\tCtrl-S")); | |
a4b59324 | 416 | menuThread->AppendSeparator(); |
ab1ca7b3 MB |
417 | menuThread->Append(THREAD_PAUSE_THREAD, _T("&Pause a running thread\tCtrl-P")); |
418 | menuThread->Append(THREAD_RESUME_THREAD, _T("&Resume suspended thread\tCtrl-R")); | |
a4b59324 | 419 | menuThread->AppendSeparator(); |
ab1ca7b3 MB |
420 | menuThread->Append(THREAD_START_WORKER, _T("Start &worker thread\tCtrl-W")); |
421 | menuBar->Append(menuThread, _T("&Thread")); | |
a4b59324 VZ |
422 | |
423 | wxMenu *menuExec = new wxMenu; | |
ab1ca7b3 MB |
424 | menuExec->Append(THREAD_EXEC_MAIN, _T("&Launch a program from main thread\tF5")); |
425 | menuExec->Append(THREAD_EXEC_THREAD, _T("L&aunch a program from a thread\tCtrl-F5")); | |
426 | menuBar->Append(menuExec, _T("&Execute")); | |
a4b59324 VZ |
427 | |
428 | wxMenu *menuHelp = new wxMenu; | |
ab1ca7b3 | 429 | menuHelp->Append(THREAD_SHOWCPUS, _T("&Show CPU count")); |
a4b59324 | 430 | menuHelp->AppendSeparator(); |
ab1ca7b3 MB |
431 | menuHelp->Append(THREAD_ABOUT, _T("&About...")); |
432 | menuBar->Append(menuHelp, _T("&Help")); | |
a4b59324 VZ |
433 | |
434 | frame->SetMenuBar(menuBar); | |
d1935bf6 | 435 | |
a6b0bd49 | 436 | // Show the frame |
bc2ec626 | 437 | frame->Show(true); |
3222fde2 | 438 | |
a6b0bd49 | 439 | SetTopWindow(frame); |
3222fde2 | 440 | |
bc2ec626 | 441 | return true; |
82052aff GL |
442 | } |
443 | ||
444 | // My frame constructor | |
bf1852e1 VZ |
445 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, |
446 | int x, int y, int w, int h) | |
bc2ec626 | 447 | : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) |
82052aff | 448 | { |
df315913 | 449 | SetIcon(wxIcon(sample_xpm)); |
d1935bf6 | 450 | |
7c3d7e2d VZ |
451 | m_nRunning = m_nCount = 0; |
452 | ||
b9de1315 VZ |
453 | m_dlgProgress = (wxProgressDialog *)NULL; |
454 | ||
8520f137 | 455 | #if wxUSE_STATUSBAR |
7c3d7e2d | 456 | CreateStatusBar(2); |
8520f137 | 457 | #endif // wxUSE_STATUSBAR |
3222fde2 | 458 | |
bc2ec626 | 459 | m_txtctrl = new wxTextCtrl(this, wxID_ANY, _T(""), wxPoint(0, 0), wxSize(0, 0), |
bee503b0 | 460 | wxTE_MULTILINE | wxTE_READONLY); |
a6b0bd49 | 461 | } |
82052aff | 462 | |
4e5bbd40 VZ |
463 | MyFrame::~MyFrame() |
464 | { | |
465 | // NB: although the OS will terminate all the threads anyhow when the main | |
466 | // one exits, it's good practice to do it ourselves -- even if it's not | |
467 | // completely trivial in this example | |
468 | ||
469 | // tell all the threads to terminate: note that they can't terminate while | |
470 | // we're deleting them because they will block in their OnExit() -- this is | |
471 | // important as otherwise we might access invalid array elements | |
4e5bbd40 | 472 | |
4e5bbd40 | 473 | { |
abbcf16d | 474 | wxCriticalSectionLocker locker(wxGetApp().m_critsect); |
4e5bbd40 | 475 | |
abbcf16d VZ |
476 | // check if we have any threads running first |
477 | const wxArrayThread& threads = wxGetApp().m_threads; | |
478 | size_t count = threads.GetCount(); | |
4e5bbd40 | 479 | |
abbcf16d VZ |
480 | if ( !count ) |
481 | return; | |
4e5bbd40 | 482 | |
abbcf16d VZ |
483 | // set the flag indicating that all threads should exit |
484 | wxGetApp().m_shuttingDown = true; | |
4e5bbd40 VZ |
485 | } |
486 | ||
abbcf16d VZ |
487 | // now wait for them to really terminate |
488 | wxGetApp().m_semAllDone.Wait(); | |
4e5bbd40 VZ |
489 | } |
490 | ||
7c3d7e2d | 491 | MyThread *MyFrame::CreateThread() |
a6b0bd49 VZ |
492 | { |
493 | MyThread *thread = new MyThread(this); | |
3222fde2 | 494 | |
bf1852e1 VZ |
495 | if ( thread->Create() != wxTHREAD_NO_ERROR ) |
496 | { | |
4693b20c | 497 | wxLogError(wxT("Can't create thread!")); |
bf1852e1 | 498 | } |
3222fde2 | 499 | |
1bd3e1ef GL |
500 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); |
501 | wxGetApp().m_threads.Add(thread); | |
bf1852e1 | 502 | |
7c3d7e2d VZ |
503 | return thread; |
504 | } | |
505 | ||
506 | void MyFrame::OnStartThreads(wxCommandEvent& WXUNUSED(event) ) | |
507 | { | |
b143cf70 | 508 | static long s_num; |
b568d04f | 509 | |
ab1ca7b3 MB |
510 | s_num = wxGetNumberFromUser(_T("How many threads to start: "), _T(""), |
511 | _T("wxThread sample"), s_num, 1, 10000, this); | |
b568d04f VZ |
512 | if ( s_num == -1 ) |
513 | { | |
514 | s_num = 10; | |
7c3d7e2d | 515 | |
7c3d7e2d | 516 | return; |
b568d04f VZ |
517 | } |
518 | ||
b143cf70 | 519 | unsigned count = unsigned(s_num), n; |
7c3d7e2d VZ |
520 | |
521 | wxArrayThread threads; | |
522 | ||
523 | // first create them all... | |
524 | for ( n = 0; n < count; n++ ) | |
525 | { | |
98f026a6 VZ |
526 | wxThread *thr = CreateThread(); |
527 | ||
528 | // we want to show the effect of SetPriority(): the first thread will | |
529 | // have the lowest priority, the second - the highest, all the rest | |
530 | // the normal one | |
531 | if ( n == 0 ) | |
532 | thr->SetPriority(WXTHREAD_MIN_PRIORITY); | |
533 | else if ( n == 1 ) | |
534 | thr->SetPriority(WXTHREAD_MAX_PRIORITY); | |
535 | else | |
536 | thr->SetPriority(WXTHREAD_DEFAULT_PRIORITY); | |
537 | ||
538 | threads.Add(thr); | |
7c3d7e2d VZ |
539 | } |
540 | ||
8520f137 | 541 | #if wxUSE_STATUSBAR |
7c3d7e2d | 542 | wxString msg; |
4693b20c | 543 | msg.Printf(wxT("%d new threads created."), count); |
7c3d7e2d | 544 | SetStatusText(msg, 1); |
8520f137 | 545 | #endif // wxUSE_STATUSBAR |
7c3d7e2d VZ |
546 | |
547 | // ...and then start them | |
548 | for ( n = 0; n < count; n++ ) | |
549 | { | |
550 | threads[n]->Run(); | |
551 | } | |
552 | } | |
553 | ||
554 | void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) ) | |
555 | { | |
556 | MyThread *thread = CreateThread(); | |
557 | ||
bf1852e1 VZ |
558 | if ( thread->Run() != wxTHREAD_NO_ERROR ) |
559 | { | |
4693b20c | 560 | wxLogError(wxT("Can't start thread!")); |
bf1852e1 | 561 | } |
7c3d7e2d | 562 | |
8520f137 | 563 | #if wxUSE_STATUSBAR |
ab1ca7b3 | 564 | SetStatusText(_T("New thread started."), 1); |
8520f137 | 565 | #endif // wxUSE_STATUSBAR |
82052aff GL |
566 | } |
567 | ||
e3e65dac | 568 | void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) ) |
82052aff | 569 | { |
abbcf16d | 570 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); |
b8b9762a | 571 | |
bf1852e1 | 572 | // stop the last thread |
1bd3e1ef | 573 | if ( wxGetApp().m_threads.IsEmpty() ) |
bee503b0 | 574 | { |
4693b20c | 575 | wxLogError(wxT("No thread to stop!")); |
bee503b0 | 576 | } |
bf1852e1 VZ |
577 | else |
578 | { | |
abbcf16d | 579 | wxGetApp().m_threads.Last()->Delete(); |
7c3d7e2d | 580 | |
8520f137 | 581 | #if wxUSE_STATUSBAR |
abbcf16d | 582 | SetStatusText(_T("Last thread stopped."), 1); |
8520f137 | 583 | #endif // wxUSE_STATUSBAR |
bf1852e1 | 584 | } |
a6b0bd49 | 585 | } |
82052aff | 586 | |
a6b0bd49 VZ |
587 | void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) ) |
588 | { | |
1bd3e1ef | 589 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); |
bee503b0 | 590 | |
3222fde2 | 591 | // resume first suspended thread |
1bd3e1ef GL |
592 | size_t n = 0, count = wxGetApp().m_threads.Count(); |
593 | while ( n < count && !wxGetApp().m_threads[n]->IsPaused() ) | |
bf1852e1 | 594 | n++; |
3222fde2 | 595 | |
bf1852e1 | 596 | if ( n == count ) |
7c3d7e2d | 597 | { |
4693b20c | 598 | wxLogError(wxT("No thread to resume!")); |
7c3d7e2d | 599 | } |
a6b0bd49 | 600 | else |
7c3d7e2d | 601 | { |
1bd3e1ef | 602 | wxGetApp().m_threads[n]->Resume(); |
7c3d7e2d | 603 | |
8520f137 | 604 | #if wxUSE_STATUSBAR |
ab1ca7b3 | 605 | SetStatusText(_T("Thread resumed."), 1); |
8520f137 | 606 | #endif // wxUSE_STATUSBAR |
7c3d7e2d | 607 | } |
82052aff GL |
608 | } |
609 | ||
e3e65dac | 610 | void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) ) |
f3855ef0 | 611 | { |
1bd3e1ef | 612 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); |
bee503b0 | 613 | |
3222fde2 | 614 | // pause last running thread |
1bd3e1ef GL |
615 | int n = wxGetApp().m_threads.Count() - 1; |
616 | while ( n >= 0 && !wxGetApp().m_threads[n]->IsRunning() ) | |
a6b0bd49 | 617 | n--; |
3222fde2 | 618 | |
a6b0bd49 | 619 | if ( n < 0 ) |
7c3d7e2d | 620 | { |
4693b20c | 621 | wxLogError(wxT("No thread to pause!")); |
7c3d7e2d | 622 | } |
a6b0bd49 | 623 | else |
7c3d7e2d | 624 | { |
1bd3e1ef | 625 | wxGetApp().m_threads[n]->Pause(); |
88ac883a | 626 | |
8520f137 | 627 | #if wxUSE_STATUSBAR |
ab1ca7b3 | 628 | SetStatusText(_T("Thread paused."), 1); |
8520f137 | 629 | #endif // wxUSE_STATUSBAR |
7c3d7e2d | 630 | } |
f3855ef0 RR |
631 | } |
632 | ||
3ccae3ba | 633 | void MyFrame::OnIdle(wxIdleEvent& event) |
3a105cbc VZ |
634 | { |
635 | DoLogThreadMessages(); | |
636 | ||
637 | UpdateThreadStatus(); | |
638 | ||
639 | event.Skip(); | |
640 | } | |
641 | ||
642 | void MyFrame::DoLogThreadMessages() | |
643 | { | |
644 | wxCriticalSectionLocker lock(m_csMessages); | |
645 | ||
646 | const size_t count = m_messages.size(); | |
647 | for ( size_t n = 0; n < count; n++ ) | |
648 | { | |
649 | m_txtctrl->AppendText(m_messages[n]); | |
650 | } | |
651 | ||
652 | m_messages.clear(); | |
653 | } | |
654 | ||
655 | void MyFrame::UpdateThreadStatus() | |
3222fde2 | 656 | { |
ffc45b67 VZ |
657 | wxCriticalSectionLocker enter(wxGetApp().m_critsect); |
658 | ||
7fe4f500 | 659 | // update the counts of running/total threads |
3222fde2 | 660 | size_t nRunning = 0, |
1bd3e1ef | 661 | nCount = wxGetApp().m_threads.Count(); |
3222fde2 VZ |
662 | for ( size_t n = 0; n < nCount; n++ ) |
663 | { | |
1bd3e1ef | 664 | if ( wxGetApp().m_threads[n]->IsRunning() ) |
3222fde2 VZ |
665 | nRunning++; |
666 | } | |
667 | ||
7c3d7e2d VZ |
668 | if ( nCount != m_nCount || nRunning != m_nRunning ) |
669 | { | |
670 | m_nRunning = nRunning; | |
671 | m_nCount = nCount; | |
672 | ||
b143cf70 | 673 | wxLogStatus(this, wxT("%u threads total, %u running."), unsigned(nCount), unsigned(nRunning)); |
7c3d7e2d VZ |
674 | } |
675 | //else: avoid flicker - don't print anything | |
f3855ef0 | 676 | } |
82052aff | 677 | |
e3e65dac | 678 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) |
82052aff | 679 | { |
bc2ec626 | 680 | Close(true); |
82052aff GL |
681 | } |
682 | ||
a4b59324 VZ |
683 | void MyFrame::OnExecMain(wxCommandEvent& WXUNUSED(event)) |
684 | { | |
4fce73fc | 685 | wxLogMessage(wxT("The exit code from the main program is %ld"), |
ab1ca7b3 | 686 | EXEC(_T("/bin/echo \"main program\""))); |
a4b59324 VZ |
687 | } |
688 | ||
689 | void MyFrame::OnExecThread(wxCommandEvent& WXUNUSED(event)) | |
690 | { | |
4fce73fc | 691 | MyExecThread thread(wxT("/bin/echo \"child thread\"")); |
a4b59324 VZ |
692 | thread.Run(); |
693 | ||
4fce73fc | 694 | wxLogMessage(wxT("The exit code from a child thread is %ld"), |
cd431efc | 695 | (long)wxPtrToUInt(thread.Wait())); |
a4b59324 VZ |
696 | } |
697 | ||
698 | void MyFrame::OnShowCPUs(wxCommandEvent& WXUNUSED(event)) | |
699 | { | |
700 | wxString msg; | |
701 | ||
702 | int nCPUs = wxThread::GetCPUCount(); | |
703 | switch ( nCPUs ) | |
704 | { | |
705 | case -1: | |
ab1ca7b3 | 706 | msg = _T("Unknown number of CPUs"); |
a4b59324 VZ |
707 | break; |
708 | ||
709 | case 0: | |
ab1ca7b3 | 710 | msg = _T("WARNING: you're running without any CPUs!"); |
a4b59324 VZ |
711 | break; |
712 | ||
713 | case 1: | |
ab1ca7b3 | 714 | msg = _T("This system only has one CPU."); |
a4b59324 VZ |
715 | break; |
716 | ||
717 | default: | |
4fce73fc | 718 | msg.Printf(wxT("This system has %d CPUs"), nCPUs); |
a4b59324 | 719 | } |
2ab25aca | 720 | |
a4b59324 VZ |
721 | wxLogMessage(msg); |
722 | } | |
723 | ||
e3e65dac | 724 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) |
82052aff | 725 | { |
2ab25aca | 726 | wxMessageDialog dialog(this, |
be5a51fb | 727 | _T("wxWidgets multithreaded application sample\n") |
ab1ca7b3 MB |
728 | _T("(c) 1998 Julian Smart, Guilhem Lavaux\n") |
729 | _T("(c) 1999 Vadim Zeitlin\n") | |
730 | _T("(c) 2000 Robert Roebling"), | |
731 | _T("About wxThread sample"), | |
3222fde2 VZ |
732 | wxOK | wxICON_INFORMATION); |
733 | ||
a6b0bd49 | 734 | dialog.ShowModal(); |
82052aff GL |
735 | } |
736 | ||
bee503b0 VZ |
737 | void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event)) |
738 | { | |
739 | m_txtctrl->Clear(); | |
740 | } | |
ce6d2511 | 741 | |
b9de1315 VZ |
742 | void MyFrame::OnUpdateWorker(wxUpdateUIEvent& event) |
743 | { | |
744 | event.Enable( m_dlgProgress == NULL ); | |
745 | } | |
746 | ||
ce6d2511 RR |
747 | void MyFrame::OnStartWorker(wxCommandEvent& WXUNUSED(event)) |
748 | { | |
749 | MyWorkerThread *thread = new MyWorkerThread(this); | |
750 | ||
751 | if ( thread->Create() != wxTHREAD_NO_ERROR ) | |
752 | { | |
4693b20c | 753 | wxLogError(wxT("Can't create thread!")); |
5ac52b5d | 754 | return; |
ce6d2511 | 755 | } |
b9de1315 VZ |
756 | |
757 | m_dlgProgress = new wxProgressDialog | |
758 | ( | |
ab1ca7b3 MB |
759 | _T("Progress dialog"), |
760 | _T("Wait until the thread terminates or press [Cancel]"), | |
b9de1315 VZ |
761 | 100, |
762 | this, | |
763 | wxPD_CAN_ABORT | | |
764 | wxPD_APP_MODAL | | |
765 | wxPD_ELAPSED_TIME | | |
766 | wxPD_ESTIMATED_TIME | | |
767 | wxPD_REMAINING_TIME | |
768 | ); | |
769 | ||
770 | // thread is not running yet, no need for crit sect | |
bc2ec626 | 771 | m_cancelled = false; |
b9de1315 | 772 | |
ce6d2511 RR |
773 | thread->Run(); |
774 | } | |
775 | ||
d48b06bd | 776 | void MyFrame::OnWorkerEvent(wxThreadEvent& event) |
ce6d2511 | 777 | { |
b9de1315 VZ |
778 | int n = event.GetInt(); |
779 | if ( n == -1 ) | |
780 | { | |
781 | m_dlgProgress->Destroy(); | |
782 | m_dlgProgress = (wxProgressDialog *)NULL; | |
783 | ||
784 | // the dialog is aborted because the event came from another thread, so | |
785 | // we may need to wake up the main event loop for the dialog to be | |
786 | // really closed | |
787 | wxWakeUpIdle(); | |
788 | } | |
789 | else | |
790 | { | |
791 | if ( !m_dlgProgress->Update(n) ) | |
792 | { | |
793 | wxCriticalSectionLocker lock(m_critsectWork); | |
794 | ||
bc2ec626 | 795 | m_cancelled = true; |
b9de1315 VZ |
796 | } |
797 | } | |
ce6d2511 RR |
798 | } |
799 | ||
b9de1315 VZ |
800 | bool MyFrame::Cancelled() |
801 | { | |
802 | wxCriticalSectionLocker lock(m_critsectWork); | |
803 | ||
804 | return m_cancelled; | |
805 | } |