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