Exemple #1
0
WorkerThread *ThreadPool::AddWorkerThread( bool bMakeAvailable, long nTimeout )
{
    QString sName = QString(m_sName + "_WorkerThread"); 
    long nThreadCount;

    LOG(VB_UPNP, LOG_INFO, QString( "ThreadPool:AddWorkerThread - %1" )
                               .arg(sName));

    WorkerThread *pThread = CreateWorkerThread( this, sName );

    if (pThread != NULL)
    {
        pThread->SetTimeout( nTimeout );
        pThread->start();

        if (pThread->WaitForInitialized( 5000 ))
        {
            // ------------------------------------------------------
            // Add new worker thread to list.
            // ------------------------------------------------------

            m_mList.lock();
            m_lstThreads.push_back( pThread );
            nThreadCount = m_lstThreads.size();

            LOG(VB_GENERAL, LOG_DEBUG,
                QString("ThreadPool:%1: thread pool size %2")
                    .arg(m_sName) .arg(nThreadCount));
                
            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.
            // (This should never happen)
            // ------------------------------------------------------

            delete pThread;
            pThread = NULL;
        }
    }

    return pThread;
}