1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets thread sample
4 // Author: Guilhem Lavaux, Vadim Zeitlin
8 // Copyright: (c) 1998-2002 wxWidgets team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #error "This sample requires thread support!"
25 #endif // wxUSE_THREADS
27 #include "wx/thread.h"
28 #include "wx/dynarray.h"
29 #include "wx/numdlg.h"
31 #include "wx/progdlg.h"
33 // define this to use wxExecute in the exec tests, otherwise just use system
37 #define EXEC(cmd) wxExecute((cmd), wxEXEC_SYNC)
39 #define EXEC(cmd) system(cmd)
43 WX_DEFINE_ARRAY_PTR(wxThread
*, wxArrayThread
);
45 // Define a new application type
46 class MyApp
: public wxApp
52 virtual bool OnInit();
55 // all the threads currently alive - as soon as the thread terminates, it's
56 // removed from the array
57 wxArrayThread m_threads
;
59 // crit section protects access to all of the arrays below
60 wxCriticalSection m_critsect
;
62 // semaphore used to wait for the threads to exit, see MyFrame::OnQuit()
63 wxSemaphore m_semAllDone
;
65 // the last exiting thread should post to m_semAllDone if this is true
66 // (protected by the same m_critsect)
67 bool m_waitingUntilAllDone
;
70 // Create a new application object
73 // Define a new frame type
74 class MyFrame
: public wxFrame
78 MyFrame(wxFrame
*frame
, const wxString
& title
, int x
, int y
, int w
, int h
);
82 void WriteText(const wxString
& text
) { m_txtctrl
->WriteText(text
); }
84 // accessors for MyWorkerThread (called in its context!)
89 void OnQuit(wxCommandEvent
& event
);
90 void OnClear(wxCommandEvent
& event
);
92 void OnStartThread(wxCommandEvent
& event
);
93 void OnStartThreads(wxCommandEvent
& event
);
94 void OnStopThread(wxCommandEvent
& event
);
95 void OnPauseThread(wxCommandEvent
& event
);
96 void OnResumeThread(wxCommandEvent
& event
);
98 void OnStartWorker(wxCommandEvent
& event
);
99 void OnWorkerEvent(wxCommandEvent
& event
);
100 void OnUpdateWorker(wxUpdateUIEvent
& event
);
102 void OnExecMain(wxCommandEvent
& event
);
103 void OnExecThread(wxCommandEvent
& event
);
105 void OnShowCPUs(wxCommandEvent
& event
);
106 void OnAbout(wxCommandEvent
& event
);
108 void OnIdle(wxIdleEvent
&event
);
111 // helper function - creates a new thread (but doesn't run it)
112 MyThread
*CreateThread();
114 // just some place to put our messages in
115 wxTextCtrl
*m_txtctrl
;
117 // remember the number of running threads and total number of threads
118 size_t m_nRunning
, m_nCount
;
120 // the progress dialog which we show while worker thread is running
121 wxProgressDialog
*m_dlgProgress
;
123 // was the worker thread cancelled by user?
126 // protects m_cancelled
127 wxCriticalSection m_critsectWork
;
129 DECLARE_EVENT_TABLE()
132 // ID for the menu commands
138 THREAD_START_THREAD
= 201,
139 THREAD_START_THREADS
,
142 THREAD_RESUME_THREAD
,
151 WORKER_EVENT
// this one gets sent from the worker thread
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 class MyThread
: public wxThread
161 MyThread(MyFrame
*frame
);
163 // thread execution starts here
164 virtual void *Entry();
166 // called when the thread exits - whether it terminates normally or is
167 // stopped with Delete() (but not when it is Kill()ed!)
168 virtual void OnExit();
170 // write something to the text control
171 void WriteText(const wxString
& text
);
178 MyThread::MyThread(MyFrame
*frame
)
185 void MyThread::WriteText(const wxString
& text
)
189 // before doing any GUI calls we must ensure that this thread is the only
195 m_frame
->WriteText(msg
);
200 void MyThread::OnExit()
202 wxCriticalSectionLocker
locker(wxGetApp().m_critsect
);
204 wxArrayThread
& threads
= wxGetApp().m_threads
;
205 threads
.Remove(this);
207 if ( threads
.IsEmpty() )
209 // signal the main thread that there are no more threads left if it is
211 if ( wxGetApp().m_waitingUntilAllDone
)
213 wxGetApp().m_waitingUntilAllDone
= false;
215 wxGetApp().m_semAllDone
.Post();
220 void *MyThread::Entry()
224 text
.Printf(wxT("Thread 0x%lx started (priority = %u).\n"),
225 GetId(), GetPriority());
227 // wxLogMessage(text); -- test wxLog thread safeness
229 for ( m_count
= 0; m_count
< 10; m_count
++ )
231 // check if we were asked to exit
235 text
.Printf(wxT("[%u] Thread 0x%lx here.\n"), m_count
, GetId());
238 // wxSleep() can't be called from non-GUI thread!
239 wxThread::Sleep(1000);
242 text
.Printf(wxT("Thread 0x%lx finished.\n"), GetId());
244 // wxLogMessage(text); -- test wxLog thread safeness
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 class MyWorkerThread
: public wxThread
256 MyWorkerThread(MyFrame
*frame
);
258 // thread execution starts here
259 virtual void *Entry();
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();
270 MyWorkerThread::MyWorkerThread(MyFrame
*frame
)
277 void MyWorkerThread::OnExit()
281 void *MyWorkerThread::Entry()
283 for ( m_count
= 0; !m_frame
->Cancelled() && (m_count
< 100); m_count
++ )
285 // check if we were asked to exit
289 // create any type of command event here
290 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, WORKER_EVENT
);
291 event
.SetInt( m_count
);
293 // send in a thread-safe way
294 wxPostEvent( m_frame
, event
);
296 // wxSleep() can't be called from non-main thread!
297 wxThread::Sleep(200);
300 wxCommandEvent
event( wxEVT_COMMAND_MENU_SELECTED
, WORKER_EVENT
);
301 event
.SetInt(-1); // that's all
302 wxPostEvent( m_frame
, event
);
307 // ----------------------------------------------------------------------------
308 // a thread which simply calls wxExecute
309 // ----------------------------------------------------------------------------
311 class MyExecThread
: public wxThread
314 MyExecThread(const wxChar
*command
) : wxThread(wxTHREAD_JOINABLE
),
320 virtual ExitCode
Entry()
322 return (ExitCode
)EXEC(m_command
);
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
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
)
342 EVT_MENU(THREAD_EXEC_MAIN
, MyFrame::OnExecMain
)
343 EVT_MENU(THREAD_EXEC_THREAD
, MyFrame::OnExecThread
)
345 EVT_MENU(THREAD_SHOWCPUS
, MyFrame::OnShowCPUs
)
346 EVT_MENU(THREAD_ABOUT
, MyFrame::OnAbout
)
348 EVT_UPDATE_UI(THREAD_START_WORKER
, MyFrame::OnUpdateWorker
)
349 EVT_MENU(THREAD_START_WORKER
, MyFrame::OnStartWorker
)
350 EVT_MENU(WORKER_EVENT
, MyFrame::OnWorkerEvent
)
352 EVT_IDLE(MyFrame::OnIdle
)
358 m_waitingUntilAllDone
= false;
365 // `Main program' equivalent, creating windows and returning main app frame
368 // uncomment this to get some debugging messages from the trace code
369 // on the console (or just set WXTRACE env variable to include "thread")
370 //wxLog::AddTraceMask("thread");
372 // Create the main frame window
373 MyFrame
*frame
= new MyFrame((wxFrame
*)NULL
, _T("wxWidgets threads sample"),
377 wxMenuBar
*menuBar
= new wxMenuBar
;
379 wxMenu
*menuFile
= new wxMenu
;
380 menuFile
->Append(THREAD_CLEAR
, _T("&Clear log\tCtrl-L"));
381 menuFile
->AppendSeparator();
382 menuFile
->Append(THREAD_QUIT
, _T("E&xit\tAlt-X"));
383 menuBar
->Append(menuFile
, _T("&File"));
385 wxMenu
*menuThread
= new wxMenu
;
386 menuThread
->Append(THREAD_START_THREAD
, _T("&Start a new thread\tCtrl-N"));
387 menuThread
->Append(THREAD_START_THREADS
, _T("Start &many threads at once"));
388 menuThread
->Append(THREAD_STOP_THREAD
, _T("S&top a running thread\tCtrl-S"));
389 menuThread
->AppendSeparator();
390 menuThread
->Append(THREAD_PAUSE_THREAD
, _T("&Pause a running thread\tCtrl-P"));
391 menuThread
->Append(THREAD_RESUME_THREAD
, _T("&Resume suspended thread\tCtrl-R"));
392 menuThread
->AppendSeparator();
393 menuThread
->Append(THREAD_START_WORKER
, _T("Start &worker thread\tCtrl-W"));
394 menuBar
->Append(menuThread
, _T("&Thread"));
396 wxMenu
*menuExec
= new wxMenu
;
397 menuExec
->Append(THREAD_EXEC_MAIN
, _T("&Launch a program from main thread\tF5"));
398 menuExec
->Append(THREAD_EXEC_THREAD
, _T("L&aunch a program from a thread\tCtrl-F5"));
399 menuBar
->Append(menuExec
, _T("&Execute"));
401 wxMenu
*menuHelp
= new wxMenu
;
402 menuHelp
->Append(THREAD_SHOWCPUS
, _T("&Show CPU count"));
403 menuHelp
->AppendSeparator();
404 menuHelp
->Append(THREAD_ABOUT
, _T("&About..."));
405 menuBar
->Append(menuHelp
, _T("&Help"));
407 frame
->SetMenuBar(menuBar
);
417 // My frame constructor
418 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
,
419 int x
, int y
, int w
, int h
)
420 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
422 m_nRunning
= m_nCount
= 0;
424 m_dlgProgress
= (wxProgressDialog
*)NULL
;
428 m_txtctrl
= new wxTextCtrl(this, wxID_ANY
, _T(""), wxPoint(0, 0), wxSize(0, 0),
429 wxTE_MULTILINE
| wxTE_READONLY
);
435 // NB: although the OS will terminate all the threads anyhow when the main
436 // one exits, it's good practice to do it ourselves -- even if it's not
437 // completely trivial in this example
439 // tell all the threads to terminate: note that they can't terminate while
440 // we're deleting them because they will block in their OnExit() -- this is
441 // important as otherwise we might access invalid array elements
444 wxGetApp().m_critsect
.Enter();
446 // check if we have any threads running first
447 const wxArrayThread
& threads
= wxGetApp().m_threads
;
448 size_t count
= threads
.GetCount();
452 // set the flag for MyThread::OnExit()
453 wxGetApp().m_waitingUntilAllDone
= true;
456 while ( ! threads
.IsEmpty() )
458 thread
= threads
.Last();
460 wxGetApp().m_critsect
.Leave();
464 wxGetApp().m_critsect
.Enter();
468 wxGetApp().m_critsect
.Leave();
472 // now wait for them to really terminate
473 wxGetApp().m_semAllDone
.Wait();
475 //else: no threads to terminate, no condition to wait for
478 MyThread
*MyFrame::CreateThread()
480 MyThread
*thread
= new MyThread(this);
482 if ( thread
->Create() != wxTHREAD_NO_ERROR
)
484 wxLogError(wxT("Can't create thread!"));
487 wxCriticalSectionLocker
enter(wxGetApp().m_critsect
);
488 wxGetApp().m_threads
.Add(thread
);
493 void MyFrame::OnStartThreads(wxCommandEvent
& WXUNUSED(event
) )
495 static long s_num
= 10;
497 s_num
= wxGetNumberFromUser(_T("How many threads to start: "), _T(""),
498 _T("wxThread sample"), s_num
, 1, 10000, this);
506 size_t count
= (size_t)s_num
, n
;
508 wxArrayThread threads
;
510 // first create them all...
511 for ( n
= 0; n
< count
; n
++ )
513 wxThread
*thr
= CreateThread();
515 // we want to show the effect of SetPriority(): the first thread will
516 // have the lowest priority, the second - the highest, all the rest
519 thr
->SetPriority(WXTHREAD_MIN_PRIORITY
);
521 thr
->SetPriority(WXTHREAD_MAX_PRIORITY
);
523 thr
->SetPriority(WXTHREAD_DEFAULT_PRIORITY
);
529 msg
.Printf(wxT("%d new threads created."), count
);
530 SetStatusText(msg
, 1);
532 // ...and then start them
533 for ( n
= 0; n
< count
; n
++ )
539 void MyFrame::OnStartThread(wxCommandEvent
& WXUNUSED(event
) )
541 MyThread
*thread
= CreateThread();
543 if ( thread
->Run() != wxTHREAD_NO_ERROR
)
545 wxLogError(wxT("Can't start thread!"));
548 SetStatusText(_T("New thread started."), 1);
551 void MyFrame::OnStopThread(wxCommandEvent
& WXUNUSED(event
) )
553 wxGetApp().m_critsect
.Enter();
555 // stop the last thread
556 if ( wxGetApp().m_threads
.IsEmpty() )
558 wxLogError(wxT("No thread to stop!"));
560 wxGetApp().m_critsect
.Leave();
564 wxThread
*thread
= wxGetApp().m_threads
.Last();
566 // it's important to leave critical section before calling Delete()
567 // because delete will (implicitly) call OnExit() which also tries
568 // to enter the same crit section - would dead lock.
569 wxGetApp().m_critsect
.Leave();
573 SetStatusText(_T("Thread stopped."), 1);
577 void MyFrame::OnResumeThread(wxCommandEvent
& WXUNUSED(event
) )
579 wxCriticalSectionLocker
enter(wxGetApp().m_critsect
);
581 // resume first suspended thread
582 size_t n
= 0, count
= wxGetApp().m_threads
.Count();
583 while ( n
< count
&& !wxGetApp().m_threads
[n
]->IsPaused() )
588 wxLogError(wxT("No thread to resume!"));
592 wxGetApp().m_threads
[n
]->Resume();
594 SetStatusText(_T("Thread resumed."), 1);
598 void MyFrame::OnPauseThread(wxCommandEvent
& WXUNUSED(event
) )
600 wxCriticalSectionLocker
enter(wxGetApp().m_critsect
);
602 // pause last running thread
603 int n
= wxGetApp().m_threads
.Count() - 1;
604 while ( n
>= 0 && !wxGetApp().m_threads
[n
]->IsRunning() )
609 wxLogError(wxT("No thread to pause!"));
613 wxGetApp().m_threads
[n
]->Pause();
615 SetStatusText(_T("Thread paused."), 1);
619 // set the frame title indicating the current number of threads
620 void MyFrame::OnIdle(wxIdleEvent
& event
)
622 wxCriticalSectionLocker
enter(wxGetApp().m_critsect
);
624 // update the counts of running/total threads
626 nCount
= wxGetApp().m_threads
.Count();
627 for ( size_t n
= 0; n
< nCount
; n
++ )
629 if ( wxGetApp().m_threads
[n
]->IsRunning() )
633 if ( nCount
!= m_nCount
|| nRunning
!= m_nRunning
)
635 m_nRunning
= nRunning
;
638 wxLogStatus(this, wxT("%u threads total, %u running."), nCount
, nRunning
);
640 //else: avoid flicker - don't print anything
645 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
650 void MyFrame::OnExecMain(wxCommandEvent
& WXUNUSED(event
))
652 wxLogMessage(wxT("The exit code from the main program is %ld"),
653 EXEC(_T("/bin/echo \"main program\"")));
656 void MyFrame::OnExecThread(wxCommandEvent
& WXUNUSED(event
))
658 MyExecThread
thread(wxT("/bin/echo \"child thread\""));
661 wxLogMessage(wxT("The exit code from a child thread is %ld"),
662 (long)thread
.Wait());
665 void MyFrame::OnShowCPUs(wxCommandEvent
& WXUNUSED(event
))
669 int nCPUs
= wxThread::GetCPUCount();
673 msg
= _T("Unknown number of CPUs");
677 msg
= _T("WARNING: you're running without any CPUs!");
681 msg
= _T("This system only has one CPU.");
685 msg
.Printf(wxT("This system has %d CPUs"), nCPUs
);
691 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
693 wxMessageDialog
dialog(this,
694 _T("wxWidgets multithreaded application sample\n")
695 _T("(c) 1998 Julian Smart, Guilhem Lavaux\n")
696 _T("(c) 1999 Vadim Zeitlin\n")
697 _T("(c) 2000 Robert Roebling"),
698 _T("About wxThread sample"),
699 wxOK
| wxICON_INFORMATION
);
704 void MyFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
709 void MyFrame::OnUpdateWorker(wxUpdateUIEvent
& event
)
711 event
.Enable( m_dlgProgress
== NULL
);
714 void MyFrame::OnStartWorker(wxCommandEvent
& WXUNUSED(event
))
716 MyWorkerThread
*thread
= new MyWorkerThread(this);
718 if ( thread
->Create() != wxTHREAD_NO_ERROR
)
720 wxLogError(wxT("Can't create thread!"));
723 m_dlgProgress
= new wxProgressDialog
725 _T("Progress dialog"),
726 _T("Wait until the thread terminates or press [Cancel]"),
732 wxPD_ESTIMATED_TIME
|
736 // thread is not running yet, no need for crit sect
742 void MyFrame::OnWorkerEvent(wxCommandEvent
& event
)
745 WriteText( _T("Got message from worker thread: ") );
746 WriteText( event
.GetString() );
747 WriteText( _T("\n") );
749 int n
= event
.GetInt();
752 m_dlgProgress
->Destroy();
753 m_dlgProgress
= (wxProgressDialog
*)NULL
;
755 // the dialog is aborted because the event came from another thread, so
756 // we may need to wake up the main event loop for the dialog to be
762 if ( !m_dlgProgress
->Update(n
) )
764 wxCriticalSectionLocker
lock(m_critsectWork
);
772 bool MyFrame::Cancelled()
774 wxCriticalSectionLocker
lock(m_critsectWork
);