]> git.saurik.com Git - wxWidgets.git/blame - samples/thread/test.cpp
OpenGl updates (still gives warnings..)
[wxWidgets.git] / samples / thread / test.cpp
CommitLineData
82052aff
GL
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
3222fde2 9// Licence: wxWindows license
82052aff
GL
10/////////////////////////////////////////////////////////////////////////////
11
bee503b0
VZ
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
82052aff 20#ifdef __GNUG__
bee503b0
VZ
21 #pragma implementation "test.cpp"
22 #pragma interface "test.cpp"
82052aff
GL
23#endif
24
25// For compilers that support precompilation, includes "wx/wx.h".
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
3222fde2 29 #pragma hdrstop
82052aff
GL
30#endif
31
32#ifndef WX_PRECOMP
3222fde2 33 #include "wx/wx.h"
82052aff
GL
34#endif
35
3222fde2
VZ
36#if !wxUSE_THREADS
37 #error "This sample requires thread support!"
38#endif // wxUSE_THREADS
39
82052aff
GL
40#include "wx/thread.h"
41#include "wx/dynarray.h"
3222fde2 42#include "wx/time.h"
82052aff 43
1bd3e1ef
GL
44class MyThread;
45WX_DEFINE_ARRAY(wxThread *, wxArrayThread);
46
82052aff 47// Define a new application type
bf1852e1 48class MyApp : public wxApp
82052aff 49{
1bd3e1ef
GL
50 public:
51 // all the threads currently alive - as soon as the thread terminates, it's
52 // removed from the array
53 wxArrayThread m_threads;
54
55 // crit section protects access to all of the arrays below
56 wxCriticalSection m_critsect;
57 public:
58 bool OnInit();
82052aff
GL
59};
60
1bd3e1ef
GL
61// Create a new application object
62IMPLEMENT_APP (MyApp)
82052aff
GL
63
64// Define a new frame type
65class MyFrame: public wxFrame
66{
a6b0bd49
VZ
67public:
68 // ctor
bf1852e1 69 MyFrame(wxFrame *frame, const wxString& title, int x, int y, int w, int h);
3222fde2 70
a6b0bd49
VZ
71 // operations
72 void WriteText(const wxString& text) { m_txtctrl->WriteText(text); }
73
74 // callbacks
82052aff
GL
75 void OnQuit(wxCommandEvent& event);
76 void OnAbout(wxCommandEvent& event);
bee503b0 77 void OnClear(wxCommandEvent& event);
a6b0bd49 78
82052aff 79 void OnStartThread(wxCommandEvent& event);
7c3d7e2d 80 void OnStartThreads(wxCommandEvent& event);
82052aff
GL
81 void OnStopThread(wxCommandEvent& event);
82 void OnPauseThread(wxCommandEvent& event);
a6b0bd49
VZ
83 void OnResumeThread(wxCommandEvent& event);
84
3222fde2 85 void OnIdle(wxIdleEvent &event);
3222fde2 86
a6b0bd49 87private:
7c3d7e2d
VZ
88 // helper function - creates a new thread (but doesn't run it)
89 MyThread *CreateThread();
88ac883a 90
7c3d7e2d 91
bf1852e1
VZ
92 // just some place to put our messages in
93 wxTextCtrl *m_txtctrl;
3222fde2 94
7c3d7e2d
VZ
95 // remember the number of running threads and total number of threads
96 size_t m_nRunning, m_nCount;
97
a6b0bd49 98 DECLARE_EVENT_TABLE()
82052aff
GL
99};
100
bee503b0 101class MyThread : public wxThread
82052aff 102{
a6b0bd49 103public:
82052aff 104 MyThread(MyFrame *frame);
3222fde2
VZ
105
106 // thread execution starts here
107 virtual void *Entry();
108
bf1852e1
VZ
109 // called when the thread exits - whether it terminates normally or is
110 // stopped with Delete() (but not when it is Kill()ed!)
bee503b0
VZ
111 virtual void OnExit();
112
3222fde2
VZ
113 // write something to the text control
114 void WriteText(const wxString& text);
a6b0bd49
VZ
115
116public:
117 size_t m_count;
82052aff
GL
118 MyFrame *m_frame;
119};
120
121MyThread::MyThread(MyFrame *frame)
a6b0bd49 122 : wxThread()
82052aff 123{
a6b0bd49
VZ
124 m_count = 0;
125 m_frame = frame;
82052aff
GL
126}
127
3222fde2
VZ
128void MyThread::WriteText(const wxString& text)
129{
130 wxString msg;
3222fde2
VZ
131
132 // before doing any GUI calls we must ensure that this thread is the only
133 // one doing it!
88ac883a 134
7b90a8f2 135 wxMutexGuiEnter();
88ac883a 136
81f1bb40 137 msg << wxTime().FormatTime() << ": " << text;
bee503b0 138
3222fde2 139 m_frame->WriteText(msg);
7b90a8f2
RR
140
141 wxMutexGuiLeave();
bee503b0
VZ
142}
143
144void MyThread::OnExit()
145{
1bd3e1ef
GL
146 wxCriticalSectionLocker locker(wxGetApp().m_critsect);
147
148 wxGetApp().m_threads.Remove(this);
3222fde2
VZ
149}
150
82052aff
GL
151void *MyThread::Entry()
152{
a6b0bd49 153 wxString text;
3222fde2 154
3222fde2
VZ
155 text.Printf("Thread 0x%x started.\n", GetID());
156 WriteText(text);
157
bee503b0 158 for ( m_count = 0; m_count < 10; m_count++ )
3222fde2
VZ
159 {
160 // check if we were asked to exit
161 if ( TestDestroy() )
162 break;
163
164 text.Printf("[%u] Thread 0x%x here.\n", m_count, GetID());
165 WriteText(text);
a6b0bd49 166
bf1852e1
VZ
167 // wxSleep() can't be called from non-GUI thread!
168 wxThread::Sleep(1000);
a6b0bd49 169 }
3222fde2
VZ
170
171 text.Printf("Thread 0x%x finished.\n", GetID());
172 WriteText(text);
173
a6b0bd49 174 return NULL;
82052aff
GL
175}
176
177// ID for the menu commands
3222fde2
VZ
178enum
179{
180 TEST_QUIT = 1,
181 TEST_TEXT = 101,
7c3d7e2d
VZ
182 TEST_ABOUT,
183 TEST_CLEAR,
184 TEST_START_THREAD = 201,
185 TEST_START_THREADS,
186 TEST_STOP_THREAD,
187 TEST_PAUSE_THREAD,
188 TEST_RESUME_THREAD
3222fde2 189};
82052aff
GL
190
191BEGIN_EVENT_TABLE(MyFrame, wxFrame)
a6b0bd49
VZ
192 EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
193 EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
bee503b0 194 EVT_MENU(TEST_CLEAR, MyFrame::OnClear)
a6b0bd49 195 EVT_MENU(TEST_START_THREAD, MyFrame::OnStartThread)
7c3d7e2d 196 EVT_MENU(TEST_START_THREADS, MyFrame::OnStartThreads)
a6b0bd49
VZ
197 EVT_MENU(TEST_STOP_THREAD, MyFrame::OnStopThread)
198 EVT_MENU(TEST_PAUSE_THREAD, MyFrame::OnPauseThread)
199 EVT_MENU(TEST_RESUME_THREAD, MyFrame::OnResumeThread)
200
3222fde2 201 EVT_IDLE(MyFrame::OnIdle)
82052aff
GL
202END_EVENT_TABLE()
203
82052aff 204// `Main program' equivalent, creating windows and returning main app frame
3222fde2 205bool MyApp::OnInit()
82052aff 206{
a6b0bd49 207 // Create the main frame window
bee503b0
VZ
208 MyFrame *frame = new MyFrame((wxFrame *)NULL, "wxWindows threads sample",
209 50, 50, 450, 340);
3222fde2 210
a6b0bd49
VZ
211 // Make a menubar
212 wxMenu *file_menu = new wxMenu;
3222fde2 213
bee503b0
VZ
214 file_menu->Append(TEST_CLEAR, "&Clear log");
215 file_menu->AppendSeparator();
a6b0bd49 216 file_menu->Append(TEST_ABOUT, "&About");
bee503b0 217 file_menu->AppendSeparator();
a6b0bd49
VZ
218 file_menu->Append(TEST_QUIT, "E&xit");
219 wxMenuBar *menu_bar = new wxMenuBar;
220 menu_bar->Append(file_menu, "&File");
3222fde2 221
a6b0bd49 222 wxMenu *thread_menu = new wxMenu;
3222fde2 223 thread_menu->Append(TEST_START_THREAD, "&Start a new thread");
7c3d7e2d 224 thread_menu->Append(TEST_START_THREADS, "Start &many threads at once");
3222fde2 225 thread_menu->Append(TEST_STOP_THREAD, "S&top a running thread");
a6b0bd49 226 thread_menu->AppendSeparator();
3222fde2
VZ
227 thread_menu->Append(TEST_PAUSE_THREAD, "&Pause a running thread");
228 thread_menu->Append(TEST_RESUME_THREAD, "&Resume suspended thread");
229 menu_bar->Append(thread_menu, "&Thread");
a6b0bd49 230 frame->SetMenuBar(menu_bar);
3222fde2 231
a6b0bd49
VZ
232 // Show the frame
233 frame->Show(TRUE);
3222fde2 234
a6b0bd49 235 SetTopWindow(frame);
3222fde2 236
a6b0bd49 237 return TRUE;
82052aff
GL
238}
239
240// My frame constructor
bf1852e1
VZ
241MyFrame::MyFrame(wxFrame *frame, const wxString& title,
242 int x, int y, int w, int h)
a6b0bd49 243 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
82052aff 244{
7c3d7e2d
VZ
245 m_nRunning = m_nCount = 0;
246
247 CreateStatusBar(2);
3222fde2 248
bee503b0
VZ
249 m_txtctrl = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(0, 0),
250 wxTE_MULTILINE | wxTE_READONLY);
82052aff 251
a6b0bd49 252}
82052aff 253
7c3d7e2d 254MyThread *MyFrame::CreateThread()
a6b0bd49
VZ
255{
256 MyThread *thread = new MyThread(this);
3222fde2 257
bf1852e1
VZ
258 if ( thread->Create() != wxTHREAD_NO_ERROR )
259 {
260 wxLogError("Can't create thread!");
261 }
3222fde2 262
1bd3e1ef
GL
263 wxCriticalSectionLocker enter(wxGetApp().m_critsect);
264 wxGetApp().m_threads.Add(thread);
bf1852e1 265
7c3d7e2d
VZ
266 return thread;
267}
268
269void MyFrame::OnStartThreads(wxCommandEvent& WXUNUSED(event) )
270{
271 static wxString s_str;
272 s_str = wxGetTextFromUser("How many threads to start: ",
273 "wxThread sample",
274 s_str, this);
275 if ( s_str.IsEmpty() )
276 return;
277
278 size_t count, n;
279 sscanf(s_str, "%u", &count);
280 if ( count == 0 )
281 return;
282
283 wxArrayThread threads;
284
285 // first create them all...
286 for ( n = 0; n < count; n++ )
287 {
288 threads.Add(CreateThread());
289 }
290
291 wxString msg;
292 msg.Printf("%d new threads created.", count);
293 SetStatusText(msg, 1);
294
295 // ...and then start them
296 for ( n = 0; n < count; n++ )
297 {
298 threads[n]->Run();
299 }
300}
301
302void MyFrame::OnStartThread(wxCommandEvent& WXUNUSED(event) )
303{
304 MyThread *thread = CreateThread();
305
bf1852e1
VZ
306 if ( thread->Run() != wxTHREAD_NO_ERROR )
307 {
308 wxLogError("Can't start thread!");
309 }
7c3d7e2d
VZ
310
311 SetStatusText("New thread started.", 1);
82052aff
GL
312}
313
e3e65dac 314void MyFrame::OnStopThread(wxCommandEvent& WXUNUSED(event) )
82052aff 315{
bf1852e1 316 // stop the last thread
1bd3e1ef 317 if ( wxGetApp().m_threads.IsEmpty() )
bee503b0
VZ
318 {
319 wxLogError("No thread to stop!");
bee503b0 320 }
bf1852e1
VZ
321 else
322 {
1bd3e1ef 323 wxGetApp().m_critsect.Enter();
7c3d7e2d 324
1bd3e1ef 325 wxThread *thread = wxGetApp().m_threads.Last();
7c3d7e2d
VZ
326
327 // it's important to leave critical section before calling Delete()
1bd3e1ef 328 // because delete will (implicitly) call OnExit() which also tries
7c3d7e2d 329 // to enter the same crit section - would dead lock.
1bd3e1ef 330 wxGetApp().m_critsect.Leave();
7c3d7e2d
VZ
331
332 thread->Delete();
333
334 SetStatusText("Thread stopped.", 1);
bf1852e1 335 }
a6b0bd49 336}
82052aff 337
a6b0bd49
VZ
338void MyFrame::OnResumeThread(wxCommandEvent& WXUNUSED(event) )
339{
1bd3e1ef 340 wxCriticalSectionLocker enter(wxGetApp().m_critsect);
bee503b0 341
3222fde2 342 // resume first suspended thread
1bd3e1ef
GL
343 size_t n = 0, count = wxGetApp().m_threads.Count();
344 while ( n < count && !wxGetApp().m_threads[n]->IsPaused() )
bf1852e1 345 n++;
3222fde2 346
bf1852e1 347 if ( n == count )
7c3d7e2d 348 {
bee503b0 349 wxLogError("No thread to resume!");
7c3d7e2d 350 }
a6b0bd49 351 else
7c3d7e2d 352 {
1bd3e1ef 353 wxGetApp().m_threads[n]->Resume();
7c3d7e2d
VZ
354
355 SetStatusText("Thread resumed.", 1);
356 }
82052aff
GL
357}
358
e3e65dac 359void MyFrame::OnPauseThread(wxCommandEvent& WXUNUSED(event) )
f3855ef0 360{
1bd3e1ef 361 wxCriticalSectionLocker enter(wxGetApp().m_critsect);
bee503b0 362
3222fde2 363 // pause last running thread
1bd3e1ef
GL
364 int n = wxGetApp().m_threads.Count() - 1;
365 while ( n >= 0 && !wxGetApp().m_threads[n]->IsRunning() )
a6b0bd49 366 n--;
3222fde2 367
a6b0bd49 368 if ( n < 0 )
7c3d7e2d 369 {
a6b0bd49 370 wxLogError("No thread to pause!");
7c3d7e2d 371 }
a6b0bd49 372 else
7c3d7e2d 373 {
1bd3e1ef 374 wxGetApp().m_threads[n]->Pause();
88ac883a 375
7c3d7e2d
VZ
376 SetStatusText("Thread paused.", 1);
377 }
f3855ef0
RR
378}
379
3222fde2
VZ
380// set the frame title indicating the current number of threads
381void MyFrame::OnIdle(wxIdleEvent &event)
382{
7fe4f500 383 // update the counts of running/total threads
3222fde2 384 size_t nRunning = 0,
1bd3e1ef 385 nCount = wxGetApp().m_threads.Count();
3222fde2
VZ
386 for ( size_t n = 0; n < nCount; n++ )
387 {
1bd3e1ef 388 if ( wxGetApp().m_threads[n]->IsRunning() )
3222fde2
VZ
389 nRunning++;
390 }
391
7c3d7e2d
VZ
392 if ( nCount != m_nCount || nRunning != m_nRunning )
393 {
394 m_nRunning = nRunning;
395 m_nCount = nCount;
396
397 wxLogStatus(this, "%u threads total, %u running.", nCount, nRunning);
398 }
399 //else: avoid flicker - don't print anything
f3855ef0 400}
82052aff 401
e3e65dac 402void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
82052aff 403{
1bd3e1ef 404 size_t count = wxGetApp().m_threads.Count();
bf1852e1
VZ
405 for ( size_t i = 0; i < count; i++ )
406 {
1bd3e1ef 407 wxGetApp().m_threads[0]->Delete();
bf1852e1 408 }
3222fde2 409
a6b0bd49 410 Close(TRUE);
82052aff
GL
411}
412
e3e65dac 413void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
82052aff 414{
bf1852e1
VZ
415 wxMessageDialog dialog(this, "wxWindows multithreaded application sample\n"
416 "(c) 1998 Julian Smart, Guilhem Lavaux\n"
417 "(c) 1999 Vadim Zeitlin",
3222fde2
VZ
418 "About wxThread sample",
419 wxOK | wxICON_INFORMATION);
420
a6b0bd49 421 dialog.ShowModal();
82052aff
GL
422}
423
bee503b0
VZ
424void MyFrame::OnClear(wxCommandEvent& WXUNUSED(event))
425{
426 m_txtctrl->Clear();
427}