#include "stdafx.h" #include "ThreadPool.h" #include "msgdef.h" #include "WorkThread.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define MIN_THREAD_NUM 4 // 最小线程数为4个 unsigned int WINAPI ThreadProc(IN LPVOID lpParam); CThreadPool::CThreadPool() { m_nMinThreadNum = 0; m_nMaxThreadNum = 0; m_nMinKeptIdleNum = 0; m_nMaxKeptIdleNum = 0; } CThreadPool::~CThreadPool() { } /************************************************************************/ /* 函数:InitThreadPool 描述:初始化线程池 参数: IN CONST int nMinNum, // 最小数量,最少为4条线程 IN CONST int nMaxNum, // 最大数量,一般为最小数量的2倍 IN CONST int nMinKeptIdleNum, // 最小保持空闲数量 IN CONST int MaxKeptIdleNum // 最大保持空闲数量,要>=最小数量 返回: */ /************************************************************************/ void CThreadPool::InitThreadPool(IN CONST int nMinNum) { m_nMinThreadNum = nMinNumStart() == 0) { --i; delete p; continue; } m_IdleList.Push_Back(p); m_ThreadList.Push_Back(p); } } /************************************************************************/ /* 函数:Termination 描述:终止 参数: 返回: */ /************************************************************************/ void CThreadPool::TerminateAll() { m_BusyList.GetQueue().clear(); m_IdleList.GetQueue().clear(); m_ThreadList.Release(); } /************************************************************************/ /* 函数:AddEventItem 描述:添加事项 参数: IN CONST LPSEVENT lpEvent 事项 返回:0成功,-1空事项,-2已经达到最大线程数量 */ /************************************************************************/ int CThreadPool::AddEventItem(IN CONST LPSEVENT lpEvent) { if(lpEvent == NULL) return -1; // 是否达到最大线程数量 if(m_ThreadList.Size() > m_nMaxThreadNum) return -2; int i = 0; int nIdleSize = m_IdleList.Size(); if(m_IdleList.Size() < m_nMinKeptIdleNum) { // 每次<最小保持空闲数量,都新创建4个 for(i=0; iStart() == 0) { --i; delete p; continue; } m_IdleList.Push_Back(p); m_ThreadList.Push_Back(p); } } // 取出空闲线程,并唤醒分醒工作 CThreadBase* pThread = m_IdleList.Pop_Front(); m_BusyList.Push_Back(pThread); ((CWorkThread*)pThread)->AddEventItem(lpEvent, this); pThread->WakeUp(); return 0; } /************************************************************************/ /* 函数:IsDeleteThread 描述:是否删除线程 参数: IN CThreadBase* pThread 线程 返回:1已删除, 0不删除,存入空闲队列,-1不存在 */ /************************************************************************/ int CThreadPool::IsDeleteThread(IN CThreadBase* pThread) { if(m_IdleList.Size() <= m_nMaxKeptIdleNum) return 0; if(m_ThreadList.Size() <= m_nMinThreadNum) return 0; m_BusyList.Remove(pThread); if(m_ThreadList.Delete(pThread) == 0) return -1; return 1; } /************************************************************************/ /* 函数:MoveToIdleList 描述:将线程移到空闲列表 参数: IN CONST CThreadBase* pThread 当前工作完成的线程 返回:1成功, 0失败 */ /************************************************************************/ int CThreadPool::MoveToIdleList(IN CThreadBase* pThread) { if(pThread == NULL) return 0; m_IdleList.Push_Back(pThread); return 1; } /************************************************************************/ /* 函数:SetMaxKeptNum 描述:设置最大保持空闲数 参数: IN CONST int nNum 数量 返回:1成功, 0失败 */ /************************************************************************/ void CThreadPool::SetMaxKeptNum(IN CONST int nNum) { CAutoLock autolock(&m_lock); m_nMaxKeptIdleNum = nNum; } /************************************************************************/ /* 函数:GetMaxKeptNum 描述:获取最大保持空闲数 参数: 返回:数量 */ /************************************************************************/ int CThreadPool::GetMaxKeptNum() { CAutoLock autolock(&m_lock); return m_nMaxKeptIdleNum; } /************************************************************************/ /* 函数:GetMaxKeptNum 描述:获取最小保持空闲数 参数: 返回:数量 */ /************************************************************************/ void CThreadPool::SetMinKeptNum(IN CONST int nNum) { CAutoLock autolock(&m_lock); m_nMinKeptIdleNum = nNum; } /************************************************************************/ /* 函数:GetMaxKeptNum 描述:获取最小保持空闲数 参数: 返回:数量 */ /************************************************************************/ int CThreadPool::GetMinKeptNum() { CAutoLock autolock(&m_lock); return m_nMinKeptIdleNum; }