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