Exemplo n.º 1
0
void ThreadPool::housekeep()
{
    m_nAge = 0;
    if (m_threads.size() <= m_nMinCapacity)
        return;

    ThreadVec idleThreads;
    ThreadVec expiredThreads;
    ThreadVec activeThreads;
    idleThreads.reserve(m_threads.size());
    activeThreads.reserve(m_threads.size());
	
    for (ThreadVec::iterator it = m_threads.begin(); it != m_threads.end(); ++it)
    {
        if ((*it)->idle())
        {
            if ((*it)->idleTime() < m_nIdleTime)
                idleThreads.push_back(*it);
            else 
                expiredThreads.push_back(*it);	
        }
        else activeThreads.push_back(*it);
    }
    size_t n = (size_t) activeThreads.size();
    size_t limit = (size_t) idleThreads.size() + n;
    if (limit < m_nMinCapacity) limit = m_nMinCapacity;
    idleThreads.insert(idleThreads.end(), expiredThreads.begin(), expiredThreads.end());
    m_threads.clear();

    for (ThreadVec::iterator it = idleThreads.begin(); it != idleThreads.end(); ++it)
    {
        if (n < limit)
        {
            m_threads.push_back(*it);
            ++n;
        }
        else (*it)->release();
    }
    m_threads.insert(m_threads.end(), activeThreads.begin(), activeThreads.end());
}
Exemplo n.º 2
0
void ThreadPool::housekeep()
{
	_age = 0;
	if (_threads.size() <= _minCapacity)
		return;

	ThreadVec idleThreads;
	ThreadVec expiredThreads;
	ThreadVec activeThreads;
	idleThreads.reserve(_threads.size());
	activeThreads.reserve(_threads.size());
	
	for (ThreadVec::iterator it = _threads.begin(); it != _threads.end(); ++it)
	{
		if ((*it)->idle())
		{
			if ((*it)->idleTime() < _idleTime)
				idleThreads.push_back(*it);
			else 
				expiredThreads.push_back(*it);	
		}
		else activeThreads.push_back(*it);
	}
	int n = (int) activeThreads.size();
	int limit = (int) idleThreads.size() + n;
	if (limit < _minCapacity) limit = _minCapacity;
	idleThreads.insert(idleThreads.end(), expiredThreads.begin(), expiredThreads.end());
	_threads.clear();
	for (ThreadVec::iterator it = idleThreads.begin(); it != idleThreads.end(); ++it)
	{
		if (n < limit)
		{
			_threads.push_back(*it);
			++n;
		}
		else (*it)->release();
	}
	_threads.insert(_threads.end(), activeThreads.begin(), activeThreads.end());
}