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

@@ -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;
}