Skip to content
Browse files

Make the worker threads clean up properly

Seems our WorkerThreads never came off the m_lstThreads deque, and also never
hit the destructor.  Fixed this in the ThreadTerminating function, and using a
"deleteLater"

Also added a bit more logging to help track the issue.
(cherry picked from commit 6a1b7e3)
  • Loading branch information...
1 parent 798359f commit 945c6731794590de8c58c638c0a1b1e40076dea2 @Beirdo Beirdo committed Jan 11, 2011
Showing with 26 additions and 24 deletions.
  1. +26 −24 mythtv/libs/libmythupnp/threadpool.cpp
View
50 mythtv/libs/libmythupnp/threadpool.cpp
@@ -367,15 +367,13 @@ WorkerThread *ThreadPool::GetWorkerThread()
// --------------------------------------------------------------
m_mList.lock();
+ if ( m_lstAvailableThreads.size() > 0)
{
- if ( m_lstAvailableThreads.size() > 0)
- {
- pThread = m_lstAvailableThreads.front();
- m_lstAvailableThreads.pop_front();
- }
-
- nThreadCount = m_lstThreads.size();
+ pThread = m_lstAvailableThreads.front();
+ m_lstAvailableThreads.pop_front();
}
+
+ nThreadCount = m_lstThreads.size();
m_mList.unlock();
if (pThread == NULL)
@@ -411,6 +409,7 @@ WorkerThread *ThreadPool::GetWorkerThread()
WorkerThread *ThreadPool::AddWorkerThread( bool bMakeAvailable, long nTimeout )
{
QString sName = m_sName + "_WorkerThread";
+ long nThreadCount;
VERBOSE( VB_UPNP, QString( "ThreadPool:AddWorkerThread - %1" ).arg( sName ));
@@ -428,23 +427,21 @@ WorkerThread *ThreadPool::AddWorkerThread( bool bMakeAvailable, long nTimeout )
// ------------------------------------------------------
m_mList.lock();
- {
+ m_lstThreads.push_back( pThread );
+ nThreadCount = m_lstThreads.size();
- m_lstThreads.push_back( pThread );
-
- if (bMakeAvailable)
- {
- m_lstAvailableThreads.push_back( pThread );
+ VERBOSE(VB_IMPORTANT|VB_EXTRA, QString("ThreadPool:%1: thread pool size %2") .arg(m_sName) .arg(nThreadCount));
- m_threadAvail.wakeAll();
- }
+ if (bMakeAvailable)
+ {
+ m_lstAvailableThreads.push_back( pThread );
+ m_threadAvail.wakeAll();
}
m_mList.unlock();
}
else
{
-
// ------------------------------------------------------
// It's taking longer than 5 seconds to initialize this thread....
// give up on it.
@@ -478,16 +475,21 @@ void ThreadPool::ThreadAvailable ( WorkerThread *pThread )
void ThreadPool::ThreadTerminating ( WorkerThread *pThread )
{
+ long nThreadCount;
+
m_mList.lock();
- {
- WorkerThreadList::iterator it =
- find(m_lstAvailableThreads.begin(),
- m_lstAvailableThreads.end(), pThread);
- m_lstAvailableThreads.erase(it);
+ WorkerThreadList::iterator it = find(m_lstAvailableThreads.begin(),
+ m_lstAvailableThreads.end(), pThread);
+ m_lstAvailableThreads.erase(it);
+
+ it = find(m_lstThreads.begin(), m_lstThreads.end(), pThread);
+ m_lstThreads.erase(it);
+
+ nThreadCount = m_lstThreads.size();
+ VERBOSE(VB_IMPORTANT|VB_EXTRA, QString("ThreadPool:%1: thread pool size %2") .arg(m_sName) .arg(nThreadCount));
+
+ pThread->deleteLater();
- // Need to leave in m_lstThreads so that we can
- // delete the ptr in destructor
- }
m_mList.unlock();
}

0 comments on commit 945c673

Please sign in to comment.
Something went wrong with that request. Please try again.