+class MyWaitingThread : public wxThread
+{
+public:
+ MyWaitingThread(wxCondition *condition)
+ {
+ m_condition = condition;
+
+ Create();
+ }
+
+ virtual ExitCode Entry()
+ {
+ printf("Thread %lu has started running.\n", 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;
+
+ // otherwise its difficult to understand which log messages pertain to
+ // which condition
+ wxLogTrace("thread", "Local condition var is %08x, gs_cond = %08x",
+ condition.GetId(), gs_cond.GetId());
+
+ // create and launch threads
+ MyWaitingThread *threads[10];
+
+ 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
+ puts("Main thread is waiting for the other threads to start");
+ fflush(stdout);
+
+ size_t nRunning = 0;
+ while ( nRunning < WXSIZEOF(threads) )
+ {
+ gs_cond.Wait();
+
+ nRunning++;
+
+ printf("Main thread: %u already running\n", nRunning);
+ fflush(stdout);
+ }
+
+ puts("Main thread: all threads started up.");
+ fflush(stdout);
+
+ wxThread::Sleep(500);
+
+#if 1
+ // now wake one of them up
+ printf("Main thread: about to signal the condition.\n");
+ fflush(stdout);
+ condition.Signal();
+#endif
+
+ wxThread::Sleep(200);
+
+ // wake all the (remaining) threads up, so that they can exit
+ printf("Main thread: about to broadcast the condition.\n");
+ fflush(stdout);
+ condition.Broadcast();
+
+ // give them time to terminate (dirty!)
+ wxThread::Sleep(500);
+}
+