?? threadpoolmodel.cpp
字號:
// ThreaoolModel.cpp: implementation of the CThreaoolModel class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ThreaoolModel.h"
#include "globfunction.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CThreaoolModel::CThreaoolModel()
{
InitAll();
}
CThreaoolModel::~CThreaoolModel()
{
DeleteAll();
}
//顯示當前線程數量
//input: void
//output: 返回線程情況串
CString CThreaoolModel::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 CThreaoolModel::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 CThreaoolModel::DeleteAll(void)
{
DestroyCriticalSection();
CloseHandle(m_hAllDeadEvent);
}
void CThreaoolModel::InitCriticalSection(void)
{
::InitializeCriticalSection(&m_csErrorNo);
}
void CThreaoolModel::DestroyCriticalSection(void)
{
::DeleteCriticalSection(&m_csErrorNo);
}
//創建新線程
//input: 線程函數指針,參數指針,線程句柄地址,線程ID地址
//output: TRUE/FASLE
BOOL CThreaoolModel::CreateNewThread(AFX_THREAROC pfnThrearoc,LPVOID pParam,
HANDLE &hNewThread,
DWORD &dwThreadId)
{
//如果當前工作線程大于或等于臨界線程量,那么我們創建新的線程
if(m_nCurAllThreadCount>=m_nMaxThreadCount)
return FALSE;
if(pfnThrearoc==NULL)
{
TPSetLastError(ERROR_THREAOOL_FULL);
return FALSE;
}
DWORD dwThreadIdTemp=0;
//創建新線程
hNewThread=chBEGINTHREADEX(NULL,0,pfnThrearoc,pParam,0,&dwThreadIdTemp);
dwThreadId=dwThreadIdTemp;
//增加當前存在線程數量
IncrementCurAllThreadCount();
return TRUE;
}
void CThreaoolModel::IncrementCurAllThreadCount(void)
{::InterlockedIncrement((LPLONG)&m_nCurAllThreadCount);}
void CThreaoolModel::DecrementCurAllThreadCount(void)
{::InterlockedDecrement((LPLONG)&m_nCurAllThreadCount);}
void CThreaoolModel::SetCurAllThreadCount(int nCount)
{::InterlockedExchange((LPLONG)&m_nCurAllThreadCount,nCount);}
void CThreaoolModel::IncrementCurWorkThreadCount(void)
{
::InterlockedIncrement((LPLONG)&m_nCurWorkThreadCount);
}
void CThreaoolModel::DecrementCurWorkThreadCount(void)
{::InterlockedDecrement((LPLONG)&m_nCurWorkThreadCount);}
void CThreaoolModel::SetCurWorkThreadCount(int nCount)
{::InterlockedExchange((LPLONG)&m_nCurWorkThreadCount,nCount);}
void CThreaoolModel::IncrementCriThreadCount(void)
{::InterlockedIncrement((LPLONG)&m_nCriThreadCount);}
void CThreaoolModel::DecrementCriThreadCount(void)
{::InterlockedDecrement((LPLONG)&m_nCriThreadCount);}
void CThreaoolModel::SetCriThreadCount(int nCount)
{::InterlockedExchange((LPLONG)&m_nCriThreadCount,nCount);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -