GH #140: Poco::Runnable threading cleanup issue

This commit is contained in:
aleks-f 2013-03-29 14:02:53 -05:00
parent ad5361ede1
commit 6719d3448f
4 changed files with 27 additions and 3 deletions

View File

@ -156,6 +156,7 @@ private:
policy(SCHED_OTHER),
done(false),
stackSize(POCO_THREAD_STACK_SIZE),
started(false),
joined(false)
{
#if defined(POCO_VXWORKS)
@ -173,6 +174,7 @@ private:
int policy;
Event done;
std::size_t stackSize;
bool started;
bool joined;
};

View File

@ -113,7 +113,7 @@ ThreadImpl::ThreadImpl():
ThreadImpl::~ThreadImpl()
{
if (!_pData->joined)
if (_pData->started && !_pData->joined)
{
pthread_detach(_pData->thread);
}
@ -224,6 +224,7 @@ void ThreadImpl::startImpl(Runnable& target)
pthread_attr_destroy(&attributes);
throw SystemException("cannot start thread");
}
_pData->started = true;
pthread_attr_destroy(&attributes);
if (_pData->policy == SCHED_OTHER)
@ -273,6 +274,7 @@ void ThreadImpl::startImpl(Callable target, void* pData)
pthread_attr_destroy(&attributes);
throw SystemException("cannot start thread");
}
_pData->started = true;
pthread_attr_destroy(&attributes);
if (_pData->policy == SCHED_OTHER)
@ -297,6 +299,7 @@ void ThreadImpl::startImpl(Callable target, void* pData)
void ThreadImpl::joinImpl()
{
if (!_pData->started) return;
_pData->done.wait();
void* result;
if (pthread_join(_pData->thread, &result))
@ -307,14 +310,16 @@ void ThreadImpl::joinImpl()
bool ThreadImpl::joinImpl(long milliseconds)
{
if (_pData->done.tryWait(milliseconds))
if (_pData->started && _pData->done.tryWait(milliseconds))
{
void* result;
if (pthread_join(_pData->thread, &result))
throw SystemException("cannot join thread");
_pData->joined = true;
return true;
}
else return false;
else if (_pData->started) return false;
else return true;
}

View File

@ -264,6 +264,19 @@ void ThreadTest::testNotJoin()
}
void ThreadTest::testNotRun()
{
Thread thread;
}
void ThreadTest::testNotRunJoin()
{
Thread thread;
thread.join();
}
void ThreadTest::testThreadTarget()
{
ThreadTarget te(&MyRunnable::staticFunc);
@ -374,6 +387,8 @@ CppUnit::Test* ThreadTest::suite()
CppUnit_addTest(pSuite, ThreadTest, testThreads);
CppUnit_addTest(pSuite, ThreadTest, testJoin);
CppUnit_addTest(pSuite, ThreadTest, testNotJoin);
CppUnit_addTest(pSuite, ThreadTest, testNotRun);
CppUnit_addTest(pSuite, ThreadTest, testNotRunJoin);
CppUnit_addTest(pSuite, ThreadTest, testThreadTarget);
CppUnit_addTest(pSuite, ThreadTest, testThreadFunction);
CppUnit_addTest(pSuite, ThreadTest, testThreadStackSize);

View File

@ -52,6 +52,8 @@ public:
void testThreads();
void testJoin();
void testNotJoin();
void testNotRun();
void testNotRunJoin();
void testThreadTarget();
void testThreadFunction();
void testThreadStackSize();