#undef TEST_ALL
static const bool TEST_ALL = TRUE;
#else
- #define TEST_HASHMAP
- #define TEST_FILENAME
+ #define TEST_THREADS
static const bool TEST_ALL = FALSE;
#endif
gs_cond.Signal();
}
-void TestDetachedThreads()
+static void TestDetachedThreads()
{
puts("\n*** Testing detached threads ***");
puts("");
}
-void TestJoinableThreads()
+static void TestJoinableThreads()
{
puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
(unsigned long)thread.Wait());
}
-void TestThreadSuspend()
+static void TestThreadSuspend()
{
puts("\n*** Testing thread suspend/resume functions ***");
puts("");
}
-void TestThreadDelete()
+static 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
puts("");
}
+// wxCondition test code
+// ----------------------------------------------------------------------------
+
+class MyWaitingThread : public wxThread
+{
+public:
+ MyWaitingThread(wxCondition *condition)
+ {
+ m_condition = condition;
+
+ Create();
+ }
+
+ virtual ExitCode Entry()
+ {
+ printf("Thread %lu has started running.", GetId());
+ fflush(stdout);
+
+ gs_cond.Signal();
+
+ printf("Thread %lu starts to wait...\n", GetId());
+ fflush(stdout);
+
+ m_condition->Wait();
+
+ printf("Thread %lu finished to wait, exiting.\n", GetId());
+ fflush(stdout);
+
+ return 0;
+ }
+
+private:
+ wxCondition *m_condition;
+};
+
+static void TestThreadConditions()
+{
+ wxCondition condition;
+
+ // create and launch threads
+ MyWaitingThread *threads[2];
+
+ size_t n;
+ for ( n = 0; n < WXSIZEOF(threads); n++ )
+ {
+ threads[n] = new MyWaitingThread(&condition);
+ }
+
+ for ( n = 0; n < WXSIZEOF(threads); n++ )
+ {
+ threads[n]->Run();
+ }
+
+ // wait until all threads run
+ printf("Main thread is waiting for the threads to start: ");
+ fflush(stdout);
+
+ size_t nRunning = 0;
+ while ( nRunning < WXSIZEOF(threads) )
+ {
+ gs_cond.Wait();
+
+ putchar('.');
+ fflush(stdout);
+
+ nRunning++;
+ }
+
+ puts("\nMain thread: all threads started up.");
+ fflush(stdout);
+
+ // now wake them up
+#if 0
+ printf("Main thread: about to signal the condition.\n");
+ fflush(stdout);
+ condition.Signal();
+#endif // 0
+
+ printf("Main thread: about to broadcast the condition.\n");
+ fflush(stdout);
+ condition.Broadcast();
+
+ // give them time to terminate (dirty)
+ wxThread::Sleep(300);
+}
+
#endif // TEST_THREADS
// ----------------------------------------------------------------------------
if ( nCPUs != -1 )
wxThread::SetConcurrency(nCPUs);
- if ( argc > 1 && argv[1][0] == 't' )
- wxLog::AddTraceMask("thread");
-
- if ( 1 )
+ if ( 0 )
+ {
TestDetachedThreads();
- if ( 1 )
TestJoinableThreads();
- if ( 1 )
TestThreadSuspend();
- if ( 1 )
TestThreadDelete();
+ }
+ TestThreadConditions();
#endif // TEST_THREADS
#ifdef TEST_LONGLONG