- putchar(m_ch);
- fflush(stdout);
-
- wxThread::Sleep(100);
- }
-
- return 0;
-}
-
-void MyDetachedThread::OnExit()
-{
- wxLogTrace("thread", "Thread %ld is in OnExit", GetId());
-
- wxCriticalSectionLocker lock(gs_critsect);
- if ( !--gs_counter )
- gs_cond.Signal();
-}
-
-void TestDetachedThreads()
-{
- puts("\n*** Testing detached threads ***");
-
- static const size_t nThreads = 3;
- MyDetachedThread *threads[nThreads];
- size_t n;
- for ( n = 0; n < nThreads; n++ )
- {
- threads[n] = new MyDetachedThread(10, 'A' + n);
- }
-
- threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
- threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
-
- for ( n = 0; n < nThreads; n++ )
- {
- threads[n]->Run();
- }
-
- // wait until all threads terminate
- gs_cond.Wait();
-
- puts("");
-}
-
-void TestJoinableThreads()
-{
- puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
-
- // calc 10! in the background
- MyJoinableThread thread(10);
- thread.Run();
-
- printf("\nThread terminated with exit code %lu.\n",
- (unsigned long)thread.Wait());
-}
-
-void TestThreadSuspend()
-{
- puts("\n*** Testing thread suspend/resume functions ***");
-
- MyDetachedThread *thread = new MyDetachedThread(15, 'X');
-
- thread->Run();
-
- // this is for this demo only, in a real life program we'd use another
- // condition variable which would be signaled from wxThread::Entry() to
- // tell us that the thread really started running - but here just wait a
- // bit and hope that it will be enough (the problem is, of course, that
- // the thread might still not run when we call Pause() which will result
- // in an error)
- wxThread::Sleep(300);
-
- for ( size_t n = 0; n < 3; n++ )
- {
- thread->Pause();
-
- puts("\nThread suspended");
- if ( n > 0 )
- {
- // don't sleep but resume immediately the first time
- wxThread::Sleep(300);
- }
- puts("Going to resume the thread");
-
- thread->Resume();
- }
-
- // wait until the thread terminates
- gs_cond.Wait();
-
- puts("");
-}
-
-void TestThreadDelete()
-{
- // As above, using Sleep() is only for testing here - we must use some
- // synchronisation object instead to ensure that the thread is still
- // running when we delete it - deleting a detached thread which already
- // terminated will lead to a crash!
-
- puts("\n*** Testing thread delete function ***");
-
- MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
-
- thread1->Run();
-
- wxThread::Sleep(300);
-
- thread1->Delete();
-
- puts("\nDeleted a running thread.");
-
- MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
-
- thread2->Run();
-
- wxThread::Sleep(300);
-
- thread2->Pause();
-
- thread2->Delete();
-
- puts("\nDeleted a sleeping thread.");
-
- MyJoinableThread *thread3 = new MyJoinableThread(20);
- thread3->Run();
-
- thread3->Delete();
-
- puts("\nDeleted a joinable thread.");
-
- MyJoinableThread *thread4 = new MyJoinableThread(2);
- thread4->Run();
-
- wxThread::Sleep(300);
-
- thread4->Delete();
-
- puts("\nDeleted a joinable thread which already terminated.");
-
- puts("");
-}
-
-#endif // TEST_THREADS
-
-// ----------------------------------------------------------------------------
-// arrays
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_ARRAYS
-
-void PrintArray(const char* name, const wxArrayString& array)
-{
- printf("Dump of the array '%s'\n", name);
-
- size_t nCount = array.GetCount();
- for ( size_t n = 0; n < nCount; n++ )
- {
- printf("\t%s[%u] = '%s'\n", name, n, array[n].c_str());
- }
-}
-
-#endif // TEST_ARRAYS
-
-// ----------------------------------------------------------------------------
-// strings
-// ----------------------------------------------------------------------------
-
-#ifdef TEST_STRINGS
-
-#include "wx/timer.h"
-
-void TestString()
-{
- wxStopWatch sw;
-
- wxString a, b, c;
-
- a.reserve (128);
- b.reserve (128);
- c.reserve (128);
-
- for (int i = 0; i < 1000000; ++i)
- {
- a = "Hello";
- b = " world";
- c = "! How'ya doin'?";
- a += b;
- a += c;
- c = "Hello world! What's up?";
- if (c != a)
- c = "Doh!";
- }
-
- printf ("TestString elapsed time: %ld\n", sw.Time());
-}
-
-void TestPChar()
-{
- wxStopWatch sw;
-
- char a [128];
- char b [128];
- char c [128];
-
- for (int i = 0; i < 1000000; ++i)
- {
- strcpy (a, "Hello");
- strcpy (b, " world");
- strcpy (c, "! How'ya doin'?");
- strcat (a, b);
- strcat (a, c);
- strcpy (c, "Hello world! What's up?");
- if (strcmp (c, a) == 0)
- strcpy (c, "Doh!");
- }
-
- printf ("TestPChar elapsed time: %ld\n", sw.Time());
-}
-
-#endif // TEST_STRINGS
-
-// ----------------------------------------------------------------------------
-// entry point
-// ----------------------------------------------------------------------------