ThreadPool fix from trunk

This commit is contained in:
Marian Krivos
2012-04-29 10:51:00 +00:00
parent b0629106ca
commit 51c8a14ae1

View File

@@ -191,8 +191,12 @@ void PooledThread::release()
_mutex.lock(); _mutex.lock();
_pTarget = 0; _pTarget = 0;
_mutex.unlock(); _mutex.unlock();
// In case of a statically allocated thread pool (such
// as the default thread pool), Windows may have already
// terminated the thread before we got here.
if (_thread.isRunning())
_targetReady.set();
_targetReady.set();
if (_thread.tryJoin(JOIN_TIMEOUT)) if (_thread.tryJoin(JOIN_TIMEOUT))
{ {
delete this; delete this;
@@ -457,25 +461,26 @@ PooledThread* ThreadPool::getThread()
PooledThread* pThread = 0; PooledThread* pThread = 0;
for (ThreadVec::iterator it = _threads.begin(); !pThread && it != _threads.end(); ++it) for (ThreadVec::iterator it = _threads.begin(); !pThread && it != _threads.end(); ++it)
{ {
if ((*it)->idle()) pThread = *it; if ((*it)->idle())
pThread = *it;
} }
if (!pThread) if (!pThread)
{ {
if (_threads.size() < _maxCapacity) if (_threads.size() < _maxCapacity)
{ {
pThread = createThread(); pThread = createThread();
try try
{ {
pThread->start(); pThread->start();
_threads.push_back(pThread); _threads.push_back(pThread);
} } catch (...)
catch (...) {
{ delete pThread;
delete pThread; throw;
throw; }
}
} }
else throw NoThreadAvailableException(); else
throw NoThreadAvailableException();
} }
pThread->activate(); pThread->activate();
return pThread; return pThread;