1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/thread/misc.cpp
3 // Purpose: Miscellaneous wxThread test cases
4 // Author: Francesco Montorsi (extracted from console sample)
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
23 #include "wx/thread.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 static size_t gs_counter
= (size_t)-1;
31 static wxCriticalSection gs_critsect
;
32 static wxSemaphore gs_cond
;
34 class MyJoinableThread
: public wxThread
37 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
38 { m_n
= n
; Create(); }
40 // thread execution starts here
41 virtual ExitCode
Entry();
47 wxThread::ExitCode
MyJoinableThread::Entry()
49 unsigned long res
= 1;
50 for ( size_t n
= 1; n
< m_n
; n
++ )
54 // it's a loooong calculation :-)
61 class MyDetachedThread
: public wxThread
64 MyDetachedThread(size_t n
, wxChar ch
)
73 // thread execution starts here
74 virtual ExitCode
Entry();
77 virtual void OnExit();
80 size_t m_n
; // number of characters to write
81 wxChar m_ch
; // character to write
83 bool m_cancelled
; // false if we exit normally
86 wxThread::ExitCode
MyDetachedThread::Entry()
89 wxCriticalSectionLocker
lock(gs_critsect
);
90 if ( gs_counter
== (size_t)-1 )
96 for ( size_t n
= 0; n
< m_n
; n
++ )
108 wxThread::Sleep(100);
114 void MyDetachedThread::OnExit()
116 //wxLogTrace(wxT("thread"), wxT("Thread %ld is in OnExit"), GetId());
118 wxCriticalSectionLocker
lock(gs_critsect
);
119 if ( !--gs_counter
&& !m_cancelled
)
123 class MyWaitingThread
: public wxThread
126 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
129 m_condition
= condition
;
134 virtual ExitCode
Entry()
136 //wxPrintf(wxT("Thread %lu has started running.\n"), GetId());
139 //wxPrintf(wxT("Thread %lu starts to wait...\n"), GetId());
145 //wxPrintf(wxT("Thread %lu finished to wait, exiting.\n"), GetId());
152 wxCondition
*m_condition
;
156 #include "wx/datetime.h"
158 class MySemaphoreThread
: public wxThread
161 MySemaphoreThread(int i
, wxSemaphore
*sem
)
162 : wxThread(wxTHREAD_JOINABLE
),
169 virtual ExitCode
Entry()
171 //wxPrintf(wxT("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
172 // wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
176 //wxPrintf(wxT("%s: Thread #%d (%ld) acquired the semaphore.\n"),
177 // wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
181 //wxPrintf(wxT("%s: Thread #%d (%ld) releasing the semaphore.\n"),
182 // wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
194 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 class MiscThreadTestCase
: public CppUnit::TestCase
203 MiscThreadTestCase();
206 CPPUNIT_TEST_SUITE( MiscThreadTestCase
);
207 CPPUNIT_TEST( TestJoinable
);
208 CPPUNIT_TEST( TestDetached
);
209 CPPUNIT_TEST( TestThreadSuspend
);
210 CPPUNIT_TEST( TestThreadDelete
);
211 CPPUNIT_TEST( TestThreadConditions
);
212 CPPUNIT_TEST( TestSemaphore
);
213 CPPUNIT_TEST_SUITE_END();
217 void TestSemaphore();
219 void TestThreadSuspend();
220 void TestThreadDelete();
221 void TestThreadConditions();
223 DECLARE_NO_COPY_CLASS(MiscThreadTestCase
)
226 // register in the unnamed registry so that these tests are run by default
227 CPPUNIT_TEST_SUITE_REGISTRATION( MiscThreadTestCase
);
229 // also include in it's own registry so that these tests can be run alone
230 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscThreadTestCase
, "MiscThreadTestCase" );
232 MiscThreadTestCase::MiscThreadTestCase()
234 int nCPUs
= wxThread::GetCPUCount();
236 wxThread::SetConcurrency(nCPUs
);
239 void MiscThreadTestCase::TestJoinable()
241 // calc 10! in the background
242 MyJoinableThread
thread(10);
243 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread
.Run() );
244 CPPUNIT_ASSERT_EQUAL( 362880, (unsigned long)thread
.Wait() );
247 void MiscThreadTestCase::TestDetached()
249 static const size_t nThreads
= 3;
250 MyDetachedThread
*threads
[nThreads
];
253 for ( n
= 0; n
< nThreads
; n
++ )
255 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
258 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
259 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
261 for ( n
= 0; n
< nThreads
; n
++ )
263 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, threads
[n
]->Run() );
266 // wait until all threads terminate
267 CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR
, gs_cond
.Wait() );
270 void MiscThreadTestCase::TestSemaphore()
272 static const int SEM_LIMIT
= 3;
274 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
275 ArrayThreads threads
;
277 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
279 threads
.Add(new MySemaphoreThread(i
, &sem
));
280 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, threads
.Last()->Run() );
283 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
285 CPPUNIT_ASSERT_EQUAL( 0, (long)threads
[n
]->Wait() );
290 void MiscThreadTestCase::TestThreadSuspend()
292 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
294 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread
->Run() );
296 // this is for this demo only, in a real life program we'd use another
297 // condition variable which would be signaled from wxThread::Entry() to
298 // tell us that the thread really started running - but here just wait a
299 // bit and hope that it will be enough (the problem is, of course, that
300 // the thread might still not run when we call Pause() which will result
302 wxThread::Sleep(300);
304 for ( size_t n
= 0; n
< 3; n
++ )
310 // don't sleep but resume immediately the first time
311 wxThread::Sleep(300);
314 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread
->Resume() );
317 // wait until the thread terminates
318 CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR
, gs_cond
.Wait() );
321 void MiscThreadTestCase::TestThreadDelete()
323 // As above, using Sleep() is only for testing here - we must use some
324 // synchronisation object instead to ensure that the thread is still
325 // running when we delete it - deleting a detached thread which already
326 // terminated will lead to a crash!
328 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
329 CPPUNIT_ASSERT_EQUAL( wxTHREAD_MISC_ERROR
, thread0
->Delete() );
330 // delete a thread which didn't start to run yet.
332 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
333 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread1
->Run() );
334 wxThread::Sleep(300);
335 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread1
->Delete() );
336 // delete a running thread
338 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
339 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread2
->Run() );
340 wxThread::Sleep(300);
341 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread2
->Pause() );
342 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread2
->Delete() );
343 // delete a sleeping thread
345 MyJoinableThread
thread3(20);
346 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread3
.Run() );
347 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread3
.Delete() );
348 // delete a joinable thread
350 MyJoinableThread
thread4(2);
351 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread4
.Run() );
352 wxThread::Sleep(300);
353 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread4
.Delete() );
354 // delete a joinable thread which already terminated
357 void MiscThreadTestCase::TestThreadConditions()
360 wxCondition
condition(mutex
);
362 // otherwise its difficult to understand which log messages pertain to
364 //wxLogTrace(wxT("thread"), wxT("Local condition var is %08x, gs_cond = %08x"),
365 // condition.GetId(), gs_cond.GetId());
367 // create and launch threads
368 MyWaitingThread
*threads
[10];
371 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
373 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
376 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
378 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, threads
[n
]->Run() );
381 // wait until all threads run
382 // NOTE: main thread is waiting for the other threads to start
384 while ( nRunning
< WXSIZEOF(threads
) )
386 CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR
, gs_cond
.Wait() );
390 // note that main thread is already running
393 wxThread::Sleep(500);
396 // now wake one of them up
397 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR
, condition
.Signal() );
400 wxThread::Sleep(200);
402 // wake all the (remaining) threads up, so that they can exit
403 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR
, condition
.Broadcast() );
405 // give them time to terminate (dirty!)
406 wxThread::Sleep(500);