WorkThreadPool.cpp
资源名称:1731.rar [点击查看]
上传用户:swkcbjrc
上传日期:2016-04-02
资源大小:45277k
文件大小:4k
源码类别:
游戏
开发平台:
Visual C++
- // WorkThreadPool.cpp: implementation of the CWorkThreadPool class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Thread.h"
- #include "threadpoolmodel.h"
- #include "WorkThreadPool.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- extern CWorkThreadPool *g_pPool;
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CWorkThreadPool::CWorkThreadPool() :
- m_exit(false,true),m_signal(false,true)
- {
- pf = NULL;
- m_threadNum = 3;
- }
- CWorkThreadPool::~CWorkThreadPool()
- {
- }
- int CWorkThreadPool::Run()
- {
- BOOL bResult=TRUE;
- SetCurAllThreadCount(3);
- SetMaxThreadCount(5);
- for(int i=0;i<m_threadNum;i++)
- {
- HANDLE hNewThread=NULL;
- DWORD dwThreadId=0;
- if(CreateNewThread(WORK,this,hNewThread,dwThreadId)==TRUE)
- {
- TRACE("Work ThreadId=%dn",dwThreadId);
- }
- else
- bResult=FALSE;
- //线程池满了
- if(TPGetLastError()==ERROR_THREADPOOL_FULL)
- return (bResult);
- }
- return 0;
- }
- //添加任务
- void CWorkThreadPool::RegisterWorkItem(LPVOID item)
- {
- m_workItems.Add(item);
- m_signal.SetEvent();
- }
- LPVOID CWorkThreadPool::GetNextItem()
- {
- LPVOID ret = NULL;
- m_lock.Lock();
- if(m_workItems.GetSize())
- ret = m_workItems[0];
- m_lock.Unlock();
- return ret;
- }
- void CWorkThreadPool::SetPF(lpCallBack1 *f)
- {
- pf = f;
- }
- UINT CWorkThreadPool::Work()
- {
- //如果全局线程池指针为空,则直接退出本线程
- if(g_pPool==NULL)
- {
- //同时从当前存在线程数中减少一个,设置ERROR_GLOBPOOL_NULL
- //全局线程错误
- g_pPool->DecrementCurAllThreadCount( );
- return ERROR_GLOBPOOL_NULL;
- }
- TRACE("Work Thread Startn");
- //取得当前线程ID
- DWORD dwThreadId=GetCurrentThreadId( );
- DWORD dwIndex=0;
- int nTemp=2;
- //声明一个事件数组用于等待事件
- LPHANDLE pEvent=new HANDLE[nTemp];
- pEvent[0]=g_pPool->GetWorkEvent(); //获取工作事件标志
- pEvent[1]=m_exit;
- while(TRUE)
- {
- //等待多事件响应,等待工作事件,死亡事件,全局死亡事件
- //如果有一个事件被置为信号态,则继续运行,否则保持等待
- dwIndex=WaitForMultipleObjects(2,pEvent,FALSE,INFINITE);
- //如果等待返回结果为错误,继续运行
- if(dwIndex==WSA_WAIT_FAILED)
- {
- Sleep(300);
- continue;
- }
- g_pPool->IncrementCurWorkThreadCount( );
- DWORD dwTemp=0;
- dwTemp=dwIndex-WSA_WAIT_EVENT_0;
- switch(dwTemp)
- {
- //dwTemp返回值为1表示线程要结束
- case 1://均为死亡
- g_pPool->DecrementCurWorkThreadCount( );
- goto CLEAR;
- break;
- case 0:
- break;
- }
- if(dwTemp!=0)
- {
- g_pPool->DecrementCurWorkThreadCount( );
- continue;
- }
- //线程开始工作
- //取得工作内容
- LPVOID pItem = GetNextItem();
- if(pItem)
- {
- RemoveWorkItem(pItem);
- pf(pItem);
- }else
- {
- m_signal.ResetEvent();
- }
- }
- CLEAR:
- if(pEvent!=NULL)
- {
- delete []pEvent;
- pEvent=NULL;
- }
- g_pPool->DecrementCurAllThreadCount( );
- return SUCCESS_THREAD_RETURN;
- }
- UINT CWorkThreadPool::WORK(LPVOID lp)
- {
- CWorkThreadPool* pWork = (CWorkThreadPool*)lp;
- ASSERT(pWork);
- pWork->Work();
- return 0;
- }
- void CWorkThreadPool::Stop()
- {
- m_signal.ResetEvent();
- m_exit.SetEvent();
- }
- //删除工作队列
- void CWorkThreadPool::RemoveWorkItem(LPVOID _pItem)
- {
- LPVOID pItem = NULL;
- m_lock.Lock();
- for(int i=0;i<m_workItems.GetSize();i++)
- {
- pItem = (LPVOID)m_workItems[i];
- if(_pItem == pItem)
- {
- m_workItems.RemoveAt(i);
- break;
- }
- }
- m_lock.Unlock();
- }
- void CWorkThreadPool::SetThreadNum(int num)
- {
- Stop();
- ASSERT(num > 0);
- m_threadNum = num;
- }