分享
 
 
 

一个好用的线程池

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

类定义如下

// ThreadPoolImp.h: interface for the ThreadPoolImp class.

//

//////////////////////////////////////////////////////////////////////

#if !defined(AFX_THREADPOOLIMP_H__82F4FC7E_2DB4_4D2A_ACC8_2EFC787CAE42__INCLUDED_)

#define AFX_THREADPOOLIMP_H__82F4FC7E_2DB4_4D2A_ACC8_2EFC787CAE42__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

#pragma warning( disable : 4705 4786)

#include <map>

#include "AutoLock.h"

using namespace std;

class IJobDesc;

class IWorker;

class CThreadPoolImp

{

public:

class ThreadInfo

{

public:

ThreadInfo() { m_hThread=0; m_bBusyWorking=false; }

ThreadInfo(HANDLE handle, bool bBusy) { m_hThread=handle; m_bBusyWorking=bBusy; }

ThreadInfo(const ThreadInfo& info) { m_hThread=info.m_hThread; m_bBusyWorking=info.m_bBusyWorking; }

////////

HANDLE m_hThread;

bool m_bBusyWorking;

};

typedef map<DWORD,ThreadInfo> ThreadInfoMap;

typedef ThreadInfoMap::iterator Iterator_ThreadInfoMap;

friend static unsigned int CThreadPoolImp::ManagerProc(void* p);

friend static unsigned int CThreadPoolImp::WorkerProc(void* p);

protected:

enum ThreadPoolStatus { BUSY, IDLE, NORMAL };

public:

//interface to the outside

void Start(unsigned short nStatic, unsigned short nmax);

void Stop(bool bHash=false);

void ProcessJob(IJobDesc* pJob, IWorker* pWorker) const;

//constructor and destructor

CThreadPoolImp();

virtual ~CThreadPoolImp();

protected:

//interfaces public:

HANDLE GetMgrIoPort() const { return m_hMgrIoPort; }

UINT GetMgrWaitTime() const { return 1000; }

HANDLE GetWorkerIoPort() const { return m_hWorkerIoPort; }

private:

static DWORD WINAPI ManagerProc(void* p);

static DWORD WINAPI WorkerProc(void* p);

protected:

//manager thread

HANDLE m_hMgrThread;

HANDLE m_hMgrIoPort;

protected:

//configuration parameters

mutable unsigned short m_nNumberOfStaticThreads;

mutable unsigned short m_nNumberOfTotalThreads;

protected:

//helper functions

void AddThreads();

void RemoveThreads();

ThreadPoolStatus GetThreadPoolStatus();

void ChangeStatus(DWORD threadId, bool status);

void RemoveThread(DWORD threadId);

protected:

//all the work threads

ThreadInfoMap m_threadMap;

CCriticalSection m_arrayCs;

HANDLE m_hWorkerIoPort;

};

#endif // !defined(AFX_THREADPOOLIMP_H__82F4FC7E_2DB4_4D2A_ACC8_2EFC787CAE42__INCLUDED_)

实现如下

// ThreadPool.cpp: implementation of the CThreadPoolImp class.

//

//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "ThreadPoolimp.h"

#include "outdebug.h"

#include <assert.h>

#include "work.h"

#ifdef _DEBUG

#undef THIS_FILE

static char THIS_FILE[]=__FILE__;

//#define new DEBUG_NEW

#endif

CThreadPoolImp::CThreadPoolImp()

{

}

CThreadPoolImp::~CThreadPoolImp()

{

}

void CThreadPoolImp::Start(unsigned short nStatic, unsigned short nMax)

{

assert(nMax>=nStatic);

HANDLE hThread;

DWORD nThreadId;

m_nNumberOfStaticThreads=nStatic;

m_nNumberOfTotalThreads=nMax;

//lock the resource

CAutoLock AutoLock(m_arrayCs);

//create an IO port

m_hMgrIoPort = CreateIoCompletionPort((HANDLE)INVALID_HANDLE_VALUE, NULL, 0, 0);

hThread = CreateThread(

NULL, // SD

0, // initial stack size

(LPTHREAD_START_ROUTINE)ManagerProc, // thread function

(LPVOID)this, // thread argument

0, // creation option

&nThreadId ); // thread identifier

m_hMgrThread = hThread;

//now we start these worker threads

m_hWorkerIoPort = CreateIoCompletionPort((HANDLE)INVALID_HANDLE_VALUE, NULL, 0, 0);

for(long n = 0; n < nStatic; n++)

{

hThread = CreateThread(

NULL, // SD

0, // initial stack size

(LPTHREAD_START_ROUTINE)WorkerProc, // thread function

(LPVOID)this, // thread argument

0, // creation option

&nThreadId );

ReportDebug("generate a worker thread handle id is %d.\n", nThreadId);

m_threadMap.insert(m_threadMap.end(),ThreadInfoMap::value_type(nThreadId,ThreadInfo(hThread, false)));

}

}

void CThreadPoolImp::Stop(bool bHash)

{

CAutoLock Lock(m_arrayCs);

::PostQueuedCompletionStatus(m_hMgrIoPort, 0, 0, (OVERLAPPED*)0xFFFFFFFF);

WaitForSingleObject(m_hMgrThread, INFINITE);

CloseHandle(m_hMgrThread);

CloseHandle(m_hMgrIoPort);

//shut down all the worker threads

UINT nCount=m_threadMap.size();

HANDLE* pThread = new HANDLE[nCount];

long n=0;

ThreadInfo info;

Iterator_ThreadInfoMap i=m_threadMap.begin();

while(i!=m_threadMap.end())

{

::PostQueuedCompletionStatus(m_hWorkerIoPort, 0, 0, (OVERLAPPED*)0xFFFFFFFF);

info=i->second;

pThread[n++]=info.m_hThread;

i++;

}

DWORD rc=WaitForMultipleObjects(nCount, pThread, TRUE, 30000);//wait for 0.5 minutes, then start to kill threads

CloseHandle(m_hWorkerIoPort);

if(rc>=WAIT_OBJECT_0 && rc<WAIT_OBJECT_0+nCount)

{

for(unsigned int n=0;n<nCount;n++)

{

CloseHandle(pThread[n]);

}

}

else if(rc==WAIT_TIMEOUT&&bHash)

{

//some threads not terminated, we have to stop them.

DWORD exitCode;

for(unsigned int i=0; i<nCount; i++)

{

if (::GetExitCodeThread(pThread[i], &exitCode)==STILL_ACTIVE)

{

TerminateThread(pThread[i], 99);

}

CloseHandle(pThread[i]);

}

}

delete[] pThread;

}

DWORD WINAPI CThreadPoolImp::ManagerProc(void* p)

{

//convert the parameter to the server pointer.

CThreadPoolImp* pServer=(CThreadPoolImp*)p;

HANDLE IoPort = pServer->GetMgrIoPort();

unsigned long pN1, pN2;

OVERLAPPED* pOverLapped;

LABEL_MANAGER_PROCESSING:

while(::GetQueuedCompletionStatus(IoPort, &pN1, &pN2,

&pOverLapped, pServer->GetMgrWaitTime() ))

{

if(pOverLapped == (OVERLAPPED*)0xFFFFFFFF)

{

return 0;

}

// else if(pOverLapped == (OVERLAPPED*)0xFFFFFFFE)

// {

// if(pN1!=0)

// {

// DWORD rc=::WaitForSingleObject((HANDLE)pN1,INFINITE);

// if(rc=WAIT_OBJECT_0)

// {

// CloseHandle((HANDLE)pN1);//关闭该线程句柄

// }

// ReportDebug("Wait a Thread Removed!\n");

// }

// }

else

{

ReportDebug("mgr events comes in!\n");

}

}

//time out processing

if (::GetLastError()==WAIT_TIMEOUT)

{

//time out processing

ReportDebug("Time out processing!\n");

//the manager will take a look at all the worker's status. The

if (pServer->GetThreadPoolStatus()==CThreadPoolImp::BUSY)

pServer->AddThreads();

if (pServer->GetThreadPoolStatus()==CThreadPoolImp::IDLE)

pServer->RemoveThreads();

goto LABEL_MANAGER_PROCESSING;

}

return 0;

}

DWORD WINAPI CThreadPoolImp::WorkerProc(void* p)

{

//convert the parameter to the server pointer.

CThreadPoolImp* pServer=(CThreadPoolImp*)p;

HANDLE IoPort = pServer->GetWorkerIoPort();

unsigned long pN1, pN2;

OVERLAPPED* pOverLapped;

DWORD threadId=::GetCurrentThreadId();

ReportDebug("worker thread id is %d.\n", threadId);

while(::GetQueuedCompletionStatus(IoPort, &pN1, &pN2,

&pOverLapped, INFINITE ))

{

if(pOverLapped == (OVERLAPPED*)0xFFFFFFFE)

{

// CThreadPoolImp::Iterator_ThreadInfoMap it=pServer->m_threadMap.find(threadId);

// if(it!=pServer->m_threadMap.end())

// {

// ::PostQueuedCompletionStatus(pServer->m_hMgrIoPort,

// (unsigned long)it->second.m_hThread,

// 0,

// (OVERLAPPED*)0xFFFFFFFE);

pServer->RemoveThread(threadId);

// ReportDebug("Try to Remove a Thread\n");

// }

break;

}

else if(pOverLapped == (OVERLAPPED*)0xFFFFFFFF)

{

break;

}

else

{

ReportDebug("worker events comes in!\n");

//before processing, we need to change the status to busy.

pServer->ChangeStatus(threadId, true);

//retrieve the job description and agent pointer

IWorker* pIWorker = reinterpret_cast<IWorker*>(pN1);

IJobDesc* pIJob= reinterpret_cast<IJobDesc*>(pN2);

pIWorker->ProcessJob(pIJob);

pServer->ChangeStatus(threadId, false);

}

}

return 0;

}

void CThreadPoolImp::ChangeStatus(DWORD threadId, bool status)

{

CAutoLock CAutoLock(m_arrayCs);

//retrieve the current thread handle

Iterator_ThreadInfoMap i;

ThreadInfo info;

i=m_threadMap.find(threadId);

info=i->second;

// m_threadMap.Lookup(threadId, info);

info.m_bBusyWorking=status;

m_threadMap.insert(m_threadMap.end(),ThreadInfoMap::value_type(threadId, info));

}

void CThreadPoolImp::ProcessJob(IJobDesc* pJob, IWorker* pWorker) const

{

::PostQueuedCompletionStatus(m_hWorkerIoPort, reinterpret_cast<DWORD>(pWorker), reinterpret_cast<DWORD>(pJob), NULL);

}

void CThreadPoolImp::AddThreads()

{

HANDLE hThread;

DWORD nThreadId;

unsigned int nCount=m_threadMap.size();

unsigned int nTotal=min(nCount+2, m_nNumberOfTotalThreads);

for(unsigned int i=0; i<nTotal-nCount; i++)

{

hThread = CreateThread(

NULL, // SD

0, // initial stack size

(LPTHREAD_START_ROUTINE)WorkerProc, // thread function

(LPVOID)this, // thread argument

0, // creation option

&nThreadId );

ReportDebug("generate a worker thread handle id is %d.\n", nThreadId);

m_threadMap.insert(m_threadMap.end(),ThreadInfoMap::value_type(nThreadId,ThreadInfo(hThread, false)));

}

}

void CThreadPoolImp::RemoveThread(DWORD threadId)

{

CAutoLock lock(m_arrayCs);

m_threadMap.erase(threadId);

}

void CThreadPoolImp::RemoveThreads()

{

unsigned int nCount=m_threadMap.size();

unsigned int nTotal=max(nCount-2, m_nNumberOfStaticThreads);

for(unsigned int i=0; i<nCount-nTotal; i++)

{

::PostQueuedCompletionStatus(m_hWorkerIoPort, 0, 0, (OVERLAPPED*)0xFFFFFFFE);

}

}

CThreadPoolImp::ThreadPoolStatus CThreadPoolImp::GetThreadPoolStatus()

{

int nTotal = m_threadMap.size();

ThreadInfo info;

int nCount=0;

Iterator_ThreadInfoMap i=m_threadMap.begin();

while(i!=m_threadMap.end())

{

info=i->second;

if (info.m_bBusyWorking==true) nCount++;

i++;

}

if ( nCount/(1.0*nTotal) > 0.8 )

return BUSY;

if ( nCount/ (1.0*nTotal) < 0.2 )

return IDLE;

return NORMAL;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有