threadpool.cpp

来自「奇迹世界公用文件源代码,研究网络游戏的朋友可以研究下」· C++ 代码 · 共 95 行

CPP
95
字号
#include "UtilityCommon.h"
#include ".\ThreadPool.h"
#include ".\IThreadInterface.h"

namespace util 
{

ThreadPool::ThreadPool()
	:	m_pThreadList	( NULL ),
		m_bAutoRelease	( TRUE )
{
	m_pThreadList = new CLinkedlist<IThreadInterface *>;
}

ThreadPool::~ThreadPool()
{
	_release();

	if( m_pThreadList )
	{
		delete m_pThreadList;
		m_pThreadList = NULL;
	}
}

VOID ThreadPool::_release()
{
	BatchStopThread();

	INT		nThreads = m_pThreadList->GetCount();
	HANDLE	* phThreads = new HANDLE[nThreads];

	int i = 0;
	IThreadInterface * pThread = NULL;
	POS pos = m_pThreadList->GetFirstPos();
	while(pos)
	{
		pThread = m_pThreadList->GetNextPos(pos);
		phThreads[i++] = pThread->GetHandle();
	}

	WaitForMultipleObjects( nThreads, phThreads, TRUE, INFINITE );

	delete phThreads;

	if( m_bAutoRelease )
	{
		BOOL bInfiniteLoop = TRUE;
		while ( bInfiniteLoop )
		{
			pThread = m_pThreadList->DeleteHead();

			if (pThread == NULL)
			{
				break;
			}

			SAFE_DELETE(pThread);
		}
	}
	else
	{
		m_pThreadList->DeleteAll();
	}
}

VOID ThreadPool::AddThread( IThreadInterface * pThread, BOOL bSuspend )
{
	m_pThreadList->AddTail( pThread );
	pThread->StartThread( bSuspend );
}

VOID ThreadPool::BatchStopThread()
{
	IThreadInterface * pThread = NULL;
	POS pos = m_pThreadList->GetFirstPos();
	while(pos)
	{
		pThread = m_pThreadList->GetNextPos(pos);
		pThread->EndThread();
	}
}

VOID ThreadPool::BatchStartThread()
{
	IThreadInterface * pThread = NULL;
	POS pos = m_pThreadList->GetFirstPos();
	while(pos)
	{
		pThread = m_pThreadList->GetNextPos(pos);
		pThread->ResumeThread();
	}
}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?