added wxCondition test
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13920 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -89,8 +89,7 @@
|
|||||||
#undef TEST_ALL
|
#undef TEST_ALL
|
||||||
static const bool TEST_ALL = TRUE;
|
static const bool TEST_ALL = TRUE;
|
||||||
#else
|
#else
|
||||||
#define TEST_HASHMAP
|
#define TEST_THREADS
|
||||||
#define TEST_FILENAME
|
|
||||||
|
|
||||||
static const bool TEST_ALL = FALSE;
|
static const bool TEST_ALL = FALSE;
|
||||||
#endif
|
#endif
|
||||||
@@ -4599,7 +4598,7 @@ void MyDetachedThread::OnExit()
|
|||||||
gs_cond.Signal();
|
gs_cond.Signal();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestDetachedThreads()
|
static void TestDetachedThreads()
|
||||||
{
|
{
|
||||||
puts("\n*** Testing detached threads ***");
|
puts("\n*** Testing detached threads ***");
|
||||||
|
|
||||||
@@ -4625,7 +4624,7 @@ void TestDetachedThreads()
|
|||||||
puts("");
|
puts("");
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestJoinableThreads()
|
static void TestJoinableThreads()
|
||||||
{
|
{
|
||||||
puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
|
puts("\n*** Testing a joinable thread (a loooong calculation...) ***");
|
||||||
|
|
||||||
@@ -4637,7 +4636,7 @@ void TestJoinableThreads()
|
|||||||
(unsigned long)thread.Wait());
|
(unsigned long)thread.Wait());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestThreadSuspend()
|
static void TestThreadSuspend()
|
||||||
{
|
{
|
||||||
puts("\n*** Testing thread suspend/resume functions ***");
|
puts("\n*** Testing thread suspend/resume functions ***");
|
||||||
|
|
||||||
@@ -4676,7 +4675,7 @@ void TestThreadSuspend()
|
|||||||
puts("");
|
puts("");
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestThreadDelete()
|
static void TestThreadDelete()
|
||||||
{
|
{
|
||||||
// As above, using Sleep() is only for testing here - we must use some
|
// As above, using Sleep() is only for testing here - we must use some
|
||||||
// synchronisation object instead to ensure that the thread is still
|
// synchronisation object instead to ensure that the thread is still
|
||||||
@@ -4732,6 +4731,92 @@ void TestThreadDelete()
|
|||||||
puts("");
|
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
|
#endif // TEST_THREADS
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@@ -5502,18 +5587,15 @@ int main(int argc, char **argv)
|
|||||||
if ( nCPUs != -1 )
|
if ( nCPUs != -1 )
|
||||||
wxThread::SetConcurrency(nCPUs);
|
wxThread::SetConcurrency(nCPUs);
|
||||||
|
|
||||||
if ( argc > 1 && argv[1][0] == 't' )
|
if ( 0 )
|
||||||
wxLog::AddTraceMask("thread");
|
{
|
||||||
|
|
||||||
if ( 1 )
|
|
||||||
TestDetachedThreads();
|
TestDetachedThreads();
|
||||||
if ( 1 )
|
|
||||||
TestJoinableThreads();
|
TestJoinableThreads();
|
||||||
if ( 1 )
|
|
||||||
TestThreadSuspend();
|
TestThreadSuspend();
|
||||||
if ( 1 )
|
|
||||||
TestThreadDelete();
|
TestThreadDelete();
|
||||||
|
}
|
||||||
|
|
||||||
|
TestThreadConditions();
|
||||||
#endif // TEST_THREADS
|
#endif // TEST_THREADS
|
||||||
|
|
||||||
#ifdef TEST_LONGLONG
|
#ifdef TEST_LONGLONG
|
||||||
|
Reference in New Issue
Block a user