]> git.saurik.com Git - wxWidgets.git/blob - samples/thread/test.cpp
wxThread fixes - compilation under Unix temporarily broken, sorry.
[wxWidgets.git] / samples / thread / test.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: test.cpp
3 // Purpose: wxWindows thread sample
4 // Author: Julian Smart(minimal)/Guilhem Lavaux(thread test)
5 // Modified by:
6 // Created: 06/16/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart, Markus Holzem, Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 /*
13 TODO:
14
15 1. show how SetPriority() works.
16 2. use worker threads to update progress controls instead of writing
17 messages - it will be more visual
18 */
19
20 #ifdef __GNUG__
21 #pragma implementation "test.cpp"
22 #pragma interface "test.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #ifndef WX_PRECOMP
33 #include "wx/wx.h"
34 #endif
35
36 #if !wxUSE_THREADS
37 #error "This sample requires thread support!"
38 #endif // wxUSE_THREADS
39
40 #include "wx/thread.h"
41 #include "wx/dynarray.h"
42 #include "wx/time.h"
43
44 // Define a new application type
45 class MyApp : public wxApp
46 {
47 public:
48 bool OnInit();
49 };
50
51 WX_DEFINE_ARRAY(wxThread *, wxArrayThread);
52
53 // Define a new frame type
54 class MyFrame: public wxFrame
55 {
56 public:
57 // ctor
58 MyFrame(wxFrame *frame, const wxString& title, int x, int y, int w, int h);
59
60 // operations
61 void WriteText(const wxString& text) { m_txtctrl->WriteText(text); }
62
63 // callbacks
64 void OnQuit(wxCommandEvent& event);
65 void OnAbout(wxCommandEvent& event);
66 void OnClear(wxCommandEvent& event);
67
68 void OnStartThread(wxCommandEvent& event);
69 void OnStopThread(wxCommandEvent& event);
70 void OnPauseThread(wxCommandEvent& event);
71 void OnResumeThread(wxCommandEvent& event);
72
73 void OnIdle(wxIdleEvent &event);
74 bool OnClose() { return TRUE; }
75
76 // called by dying thread
77 void OnThreadExit(wxThread *thread);
78
79 public:
80 wxArrayThread m_threads;
81
82 private:
83 // crit section protects access to the array below
84 wxCriticalSection m_critsect;
85
86 // the array of threads which finished (either because they did their work
87 // or because they were explicitly stopped)
88 wxArrayInt m_aToDelete;
89
90 // just some place to put our messages in
91 wxTextCtrl *m_txtctrl;
92
93 DECLARE_EVENT_TABLE()
94 };
95
96 class MyThread : public wxThread
97 {
98 public:
99 MyThread(MyFrame *frame);
100
101 // thread execution starts here
102 virtual void *Entry();
103
104 // called when the thread exits - whether it terminates normally or is
105 // stopped with Delete() (but not when it is Kill()ed!)
106 virtual void OnExit();
107
108 // write something to the text control
109 void WriteText(const wxString& text);
110
111 public:
112 size_t m_count;
113 MyFrame *m_frame;
114 };
115
116 MyThread::MyThread(MyFrame *frame)
117 : wxThread()
118 {
119 m_count = 0;
120 m_frame = frame;
121 }
122
123 void MyThread::WriteText(const wxString& text)
124 {
125 wxString msg;
126 msg << wxTime().FormatTime() << ": " << text;
127
128 // before doing any GUI calls we must ensure that this thread is the only
129 // one doing it!
130 wxMutexGuiLocker guiLocker;
131
132 m_frame->WriteText(msg);
133 }
134
135 void MyThread::OnExit()
136 {
137 m_frame->OnThreadExit(this);
138 }
139
140 void *MyThread::Entry()
141 {
142 wxString text;
143
144 text.Printf("Thread 0x%x started.\n", GetID());
145 WriteText(text);
146
147 for ( m_count = 0; m_count < 10; m_count++ )
148 {
149 // check if we were asked to exit
150 if ( TestDestroy() )
151 break;
152
153 text.Printf("[%u] Thread 0x%x here.\n", m_count, GetID());
154 WriteText(text);
155
156 // wxSleep() can't be called from non-GUI thread!
157 wxThread::Sleep(1000);
158 }
159
160 text.Printf("Thread 0x%x finished.\n", GetID());
161 WriteText(text);
162
163 return NULL;
164 }
165
166 // ID for the menu commands
167 enum
168 {
169 TEST_QUIT = 1,
170 TEST_TEXT = 101,
171 TEST_ABOUT = 102,
172 TEST_CLEAR = 103,
173 TEST_START_THREAD = 203,
174 TEST_STOP_THREAD = 204,
175 TEST_PAUSE_THREAD = 205,
176 TEST_RESUME_THREAD = 206
177 };
178
179 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
180 EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
181 EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
182 EVT_MENU(TEST_CLEAR, MyFrame::OnClear)
183 EVT_MENU(TEST_START_THREAD, MyFrame::OnStartThread)
184 EVT_MENU(TEST_STOP_THREAD, MyFrame::OnStopThread)
185 EVT_MENU(TEST_PAUSE_THREAD, MyFrame::OnPauseThread)
186 EVT_MENU(TEST_RESUME_THREAD, MyFrame::OnResumeThread)
187
188 EVT_IDLE(MyFrame::OnIdle)
189 END_EVENT_TABLE()
190
191 // Create a new application object
192 IMPLEMENT_APP (MyApp)
193
194 // `Main program' equivalent, creating windows and returning main app frame
195 bool MyApp::OnInit()
196 {
197 // Create the main frame window
198 MyFrame *frame = new MyFrame((wxFrame *)NULL, "wxWindows threads sample",
199 50, 50, 450, 340);
200
201 // Make a menubar
202 wxMenu *file_menu = new wxMenu;
203
204 file_menu->Append(TEST_CLEAR, "&Clear log");
205 file_menu->AppendSeparator();
206 file_menu->Append(TEST_ABOUT, "&About");
207 file_menu->AppendSeparator();
208 file_menu->Append(TEST_QUIT, "E&xit");
209 wxMenuBar *menu_bar = new wxMenuBar;
210 menu_bar->Append(file_menu, "&File");
211
212 wxMenu *thread_menu = new wxMenu;
213 thread_menu->Append(TEST_START_THREAD, "&Start a new thread");
214 thread_menu->Append(TEST_STOP_THREAD, "S&top a running thread");
215 thread_menu->AppendSeparator();
216 thread_menu->Append(TEST_PAUSE_THREAD, "&Pause a running thread");
217 thread_menu->Append(TEST_RESUME_THREAD, "&Resume suspended thread");
218 menu_bar->Append(thread_menu, "&Thread");
219 frame->SetMenuBar(menu_bar);
220
221 // Show the frame
222 frame->Show(TRUE);
223
224 SetTopWindow(frame);
225
226 return TRUE;
227 }
228
229 // My frame constructor
230 MyFrame::MyFrame(wxFrame *frame, const wxString& title,
231 int x, int y, int w, int h)
232 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
233 {
234 CreateStatusBar();
235
236 m_txtctrl = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(0, 0),
237 wxTE_MULTILINE | wxTE_READONLY);
238
239 }
240
241 void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) )
242 {
243 MyThread *thread = new MyThread(this);
244
245 if ( thread->Create() != wxTHREAD_NO_ERROR )
246 {
247 wxLogError("Can't create thread!");
248 }
249
250 wxCriticalSectionLocker enter(m_critsect);
251 m_threads.Add(thread);
252
253 if ( thread->Run() != wxTHREAD_NO_ERROR )
254 {
255 wxLogError("Can't start thread!");
256 }
257 }
258
259 void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) )
260 {
261 // stop the last thread
262 if ( m_threads.IsEmpty() )
263 {
264 wxLogError("No thread to stop!");
265 }
266 else
267 {
268 m_threads.Last()->Delete();
269 }
270 }
271
272 void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) )
273 {
274 wxCriticalSectionLocker enter(m_critsect);
275
276 // resume first suspended thread
277 size_t n = 0, count = m_threads.Count();
278 while ( n < count && !m_threads[n]->IsPaused() )
279 n++;
280
281 if ( n == count )
282 wxLogError("No thread to resume!");
283 else
284 m_threads[n]->Resume();
285 }
286
287 void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
288 {
289 wxCriticalSectionLocker enter(m_critsect);
290
291 // pause last running thread
292 int n = m_threads.Count() - 1;
293 while ( n >= 0 && !m_threads[n]->IsRunning() )
294 n--;
295
296 if ( n < 0 )
297 wxLogError("No thread to pause!");
298 else
299 m_threads[n]->Pause();
300 }
301
302 // set the frame title indicating the current number of threads
303 void MyFrame::OnIdle(wxIdleEvent &event)
304 {
305 // first remove from the array all the threads which died since last call
306 {
307 wxCriticalSectionLocker enter(m_critsect);
308
309 size_t nCount = m_aToDelete.Count();
310 for ( size_t n = 0; n < nCount; n++ )
311 {
312 // index should be shifted by n because we've already deleted
313 // n-1 elements from the array
314 m_threads.Remove((size_t)m_aToDelete[n] - n);
315 }
316
317 m_aToDelete.Empty();
318 }
319
320 size_t nRunning = 0,
321 nCount = m_threads.Count();
322 for ( size_t n = 0; n < nCount; n++ )
323 {
324 if ( m_threads[n]->IsRunning() )
325 nRunning++;
326 }
327
328 wxLogStatus(this, "%u threads total, %u running.", nCount, nRunning);
329 }
330
331 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
332 {
333 size_t count = m_threads.Count();
334 for ( size_t i = 0; i < count; i++ )
335 {
336 m_threads[i]->Delete();
337 }
338
339 Close(TRUE);
340 }
341
342 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
343 {
344 wxMessageDialog dialog(this, "wxWindows multithreaded application sample\n"
345 "(c) 1998 Julian Smart, Guilhem Lavaux\n"
346 "(c) 1999 Vadim Zeitlin",
347 "About wxThread sample",
348 wxOK | wxICON_INFORMATION);
349
350 dialog.ShowModal();
351 }
352
353 void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
354 {
355 m_txtctrl->Clear();
356 }
357
358 void MyFrame::OnThreadExit(wxThread *thread)
359 {
360 int index = m_threads.Index(thread);
361 wxCHECK_RET( index != -1, "unknown thread being deleted??" );
362
363 wxCriticalSectionLocker enter(m_critsect);
364
365 m_aToDelete.Add(index);
366 }