+// 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);
+}
+