+#include "wx/utils.h"
+
+class MyExecThread : public wxThread
+{
+public:
+ MyExecThread(const wxString& command) : wxThread(wxTHREAD_JOINABLE),
+ m_command(command)
+ {
+ Create();
+ }
+
+ virtual ExitCode Entry()
+ {
+ return (ExitCode)wxExecute(m_command, wxEXEC_SYNC);
+ }
+
+private:
+ wxString m_command;
+};
+
+static void TestThreadExec()
+{
+ wxPuts(_T("*** Testing wxExecute interaction with threads ***\n"));
+
+ MyExecThread thread(_T("true"));
+ thread.Run();
+
+ wxPrintf(_T("Main program exit code: %ld.\n"),
+ wxExecute(_T("false"), wxEXEC_SYNC));
+
+ wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread.Wait());
+}
+
+// semaphore tests
+#include "wx/datetime.h"
+
+class MySemaphoreThread : public wxThread
+{
+public:
+ MySemaphoreThread(int i, wxSemaphore *sem)
+ : wxThread(wxTHREAD_JOINABLE),
+ m_sem(sem),
+ m_i(i)
+ {
+ Create();
+ }
+
+ virtual ExitCode Entry()
+ {
+ wxPrintf(_T("%s: Thread %d starting to wait for semaphore...\n"),
+ wxDateTime::Now().FormatTime().c_str(), m_i);
+
+ m_sem->Wait();
+
+ wxPrintf(_T("%s: Thread %d acquired the semaphore.\n"),
+ wxDateTime::Now().FormatTime().c_str(), m_i);
+
+ Sleep(1000);
+
+ wxPrintf(_T("%s: Thread %d releasing the semaphore.\n"),
+ wxDateTime::Now().FormatTime().c_str(), m_i);
+
+ m_sem->Post();
+
+ return 0;
+ }
+
+private:
+ wxSemaphore *m_sem;
+ int m_i;
+};
+
+WX_DEFINE_ARRAY(wxThread *, ArrayThreads);
+
+static void TestSemaphore()
+{
+ wxPuts(_T("*** Testing wxSemaphore class. ***"));
+
+ static const int SEM_LIMIT = 3;
+
+ wxSemaphore sem(SEM_LIMIT, SEM_LIMIT);
+ ArrayThreads threads;
+
+ for ( int i = 0; i < 3*SEM_LIMIT; i++ )
+ {
+ threads.Add(new MySemaphoreThread(i, &sem));
+ threads.Last()->Run();
+ }
+
+ for ( size_t n = 0; n < threads.GetCount(); n++ )
+ {
+ threads[n]->Wait();
+ delete threads[n];
+ }
+}
+