www.pudn.com > 0818ThreadPool.rar > ThreadPoolModel.cpp
// ThreaoolModel.cpp: implementation of the CThreadPoolModel class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ThreadpoolModel.h"
#include "globfunction.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
const DWORD _THREADDEAD_TIMEOUT= 4*1000;// 4秒线程死亡超时
CThreadPoolModel::CThreadPoolModel()
{
InitAll();
}
CThreadPoolModel::~CThreadPoolModel()
{
DeleteAll();
}
//显示当前线程数量
//input: void
//output: 返回线程情况串
CString CThreadPoolModel::ShowThreadInfo(void)
{
CString strShow;
strShow.Format(
"最大线程=%d,当前线程=%d,工作线程=%d,临界线程=%d\n",
m_nMaxThreadCount,m_nCurAllThreadCount,
m_nCurWorkThreadCount,m_nCriThreadCount);
return strShow;
}
//初始化所有信息
//input: void
//output: void
void CThreadPoolModel::InitAll(void)
{
InitCriticalSection();
m_nMaxThreadCount=0;
m_nCurAllThreadCount=0;
m_nCurWorkThreadCount=0;
m_nErrorNo=0;
m_hAllDeadEvent=NULL;
m_hMainWnd=NULL;
m_hAllDeadEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
m_nCriThreadCount=0;
}
//删除所有包括句柄
//input: void
//output: void
void CThreadPoolModel::DeleteAll(void)
{
DestroyCriticalSection();
CloseHandle(m_hAllDeadEvent);
}
void CThreadPoolModel::InitCriticalSection(void)
{
::InitializeCriticalSection(&m_csErrorNo);
}
void CThreadPoolModel::DestroyCriticalSection(void)
{
::DeleteCriticalSection(&m_csErrorNo);
}
//创建新线程
//input: 线程函数指针,参数指针,线程句柄地址,线程ID地址
//output: TRUE/FASLE
BOOL CThreadPoolModel::CreateNewThread(lpCallBack *pfnThrearoc ,LPVOID pParam,
HANDLE &hNewThread,
DWORD &dwThreadId)
{
//如果当前工作线程大于或等于临界线程量,那么我们创建新的线程
if(m_nCurAllThreadCount>=m_nMaxThreadCount)
return FALSE;
if(pfnThrearoc==NULL)
{
TPSetLastError(ERROR_THREADPOOL_FULL);
return FALSE;
}
DWORD dwThreadIdTemp=0;
//创建新线程
hNewThread=chBEGINTHREADEX(NULL,0,pfnThrearoc,pParam,0,&dwThreadIdTemp);
dwThreadId=dwThreadIdTemp;
//增加当前存在线程数量
IncrementCurAllThreadCount();
return TRUE;
}
void CThreadPoolModel::IncrementCurAllThreadCount(void)
{::InterlockedIncrement((LPLONG)&m_nCurAllThreadCount);}
void CThreadPoolModel::DecrementCurAllThreadCount(void)
{::InterlockedDecrement((LPLONG)&m_nCurAllThreadCount);}
void CThreadPoolModel::SetCurAllThreadCount(int nCount)
{::InterlockedExchange((LPLONG)&m_nCurAllThreadCount,nCount);}
void CThreadPoolModel::IncrementCurWorkThreadCount(void)
{
::InterlockedIncrement((LPLONG)&m_nCurWorkThreadCount);
}
void CThreadPoolModel::DecrementCurWorkThreadCount(void)
{::InterlockedDecrement((LPLONG)&m_nCurWorkThreadCount);}
void CThreadPoolModel::SetCurWorkThreadCount(int nCount)
{::InterlockedExchange((LPLONG)&m_nCurWorkThreadCount,nCount);}
void CThreadPoolModel::IncrementCriThreadCount(void)
{::InterlockedIncrement((LPLONG)&m_nCriThreadCount);}
void CThreadPoolModel::DecrementCriThreadCount(void)
{::InterlockedDecrement((LPLONG)&m_nCriThreadCount);}
void CThreadPoolModel::SetCriThreadCount(int nCount)
{::InterlockedExchange((LPLONG)&m_nCriThreadCount,nCount);}