⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadpoolmodel.cpp

📁 <VC++网络游戏建摸与实现>源代码
💻 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 + -