if (m_pThread) // does the thread still exist?
{
- m_out.Printf("MYFRAME: deleting thread");
+ wxMessageOutputDebug().Printf("MYFRAME: deleting thread");
if (m_pThread->Delete() != wxTHREAD_NO_ERROR )
wxLogError("Can't delete the thread!");
of detached threads.
This function can only be called from another thread context.
+
+ Finally, note that once a thread has completed and its Entry() function
+ returns, you cannot call Run() on it again (an assert will fail in debug
+ builds or @c wxTHREAD_RUNNING will be returned in release builds).
*/
wxThreadError Run();
{
wxCriticalSectionLocker lock(m_critsect);
- if ( m_internal->GetState() != STATE_NEW )
- {
- // actually, it may be almost any state at all, not only STATE_RUNNING
- return wxTHREAD_RUNNING;
- }
+ wxCHECK_MSG( m_internal->GetState() == STATE_NEW, wxTHREAD_RUNNING,
+ wxT("thread may only be started once after Create()") );
// the thread has just been created and is still suspended - let it run
return Resume();
CPPUNIT_TEST( TestDetached );
CPPUNIT_TEST( TestThreadSuspend );
CPPUNIT_TEST( TestThreadDelete );
+ CPPUNIT_TEST( TestThreadRun );
CPPUNIT_TEST( TestThreadConditions );
CPPUNIT_TEST( TestSemaphore );
CPPUNIT_TEST_SUITE_END();
void TestThreadSuspend();
void TestThreadDelete();
+ void TestThreadRun();
void TestThreadConditions();
DECLARE_NO_COPY_CLASS(MiscThreadTestCase)
void MiscThreadTestCase::TestThreadDelete()
{
+ // FIXME:
// As above, using Sleep() is only for testing here - we must use some
// synchronisation object instead to ensure that the thread is still
// running when we delete it - deleting a detached thread which already
MyJoinableThread thread3(20);
CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread3.Run() );
CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread3.Delete() );
- // delete a joinable thread
+ // delete a joinable running thread
MyJoinableThread thread4(2);
CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread4.Run() );
// delete a joinable thread which already terminated
}
+void MiscThreadTestCase::TestThreadRun()
+{
+ MyJoinableThread thread1(2);
+ CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread1.Run() );
+ thread1.Wait(); // wait until the thread ends
+
+ // verify that running twice the same thread fails
+ WX_ASSERT_FAILS_WITH_ASSERT( thread1.Run() );
+}
+
void MiscThreadTestCase::TestThreadConditions()
{
wxMutex mutex;