Chinaunix首页 | 论坛 | 博客
  • 博客访问: 818742
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:10:35

#ifndef  _THREADPOOLMODLE_H_
#define  _THREADPOOLMODLE_H_

/*===========================================================================
@ File: threadpoolmodle.h
@ Date: 2006/3/13
@ Author: zhao.xj
@ Version: 1.0.0
@ description: 本类实现了一个简单的线程池,需要用的可以继承本类,实现函数run,来
               完成自己的特殊工作
=============================================================================
*/


#include 
<assert.h>
#include 
<process.h>

class ThreadPoolModle
{
 friend unsigned 
int  _stdcall ThreadProc(void* lpParam);
 
bool isPoolEnded;
 
int m_nNum;
 HANDLE
* m_pthreads;
 HANDLE killAllThreads;

private:
 ThreadPoolModle(
const ThreadPoolModle& threadModle);
 ThreadPoolModle
& operator = (const ThreadPoolModle& threadModle);

protected:
 
void* m_thrdPara;  //线程参数

 
//线程处理的函数,使用线程池的类继承并实现它
 virtual unsigned int Run() = 0;  

 
//在线程函数中必须检查这个函数,以判断是否停止线程
 bool CheckThrExit();

public:
 
//默认是只要一个线程的对象
 ThreadPoolModle(int num = 1)  
 
{
  assert(num 
>= 1);
  m_nNum 
= num;
  m_pthreads 
= new HANDLE[m_nNum];
  m_thrdPara 
= NULL;
  isPoolEnded 
= false;
  killAllThreads 
= CreateEvent(NULL, truefalse, NULL);
 }


 
virtual ~ThreadPoolModle(void)
 
{
  
if (!isPoolEnded)
  
{
   EndThreads();
      CloseHandle(killAllThreads );
  }

  delete []m_pthreads;
 }


 
//得到线程池中的线程数
 int ThreadNum()const {return m_nNum;}
    
//启动线程池所有的线程
 bool StartThreads(LPVOID thrdPara);
 
//停止线程池所有的线程
 void EndThreads();

 
//挂起所有线程
 void SuspendThreads();
 
//恢复所有线程
    void ResumeThreads();
 
//设置线程优先级
 void SetThreadsPriority(int nPriority);
 
//得到线程优先级
 int  GetThreadsPriority();
}
;

#endif

//////////////////////////////////////////////////////////////////////////////////threadpoolmodle.cpp//////////////////////////////////////////////////////////////
#include "threadpoolmodle.h"
bool ThreadPoolModle::CheckThrExit()
{
 
if(WaitForSingleObject(killAllThreads, 0== WAIT_OBJECT_0)
  
return true;
 
else
  
return false;
}


bool ThreadPoolModle::StartThreads(void* thrdPara)
{
 m_thrdPara 
= thrdPara;
 
for (int i = 0; i < m_nNum; ++i)
 
{
  m_pthreads[i] 
= (HANDLE)_beginthreadex(NULL, 0, ThreadProc, this0, NULL);
  
if (m_pthreads[i] == NULL)
  
{
   EndThreads();
   
return false;
  }

 }


 
return true;
}


void ThreadPoolModle::EndThreads()
{
 
int haveExit = 0;
 SetEvent(killAllThreads);
 WaitForMultipleObjects(m_nNum, m_pthreads, 
true, INFINITE);
 
for(int i = 0 ; i < m_nNum; ++i)
 
{
  CloseHandle(m_pthreads[i]);
 }

 isPoolEnded 
= true;
}


void ThreadPoolModle::SuspendThreads()
{
 
for(int i = 0 ; i < m_nNum; ++i)
 
{
  SuspendThread(m_pthreads[i]);
 }

}


void ThreadPoolModle::ResumeThreads()
{
 
for(int i = 0 ; i < m_nNum; ++i)
 
{
  ResumeThread(m_pthreads[i]);
 }

}


void ThreadPoolModle::SetThreadsPriority(int nPriority)
{
 
for(int i = 0 ; i < m_nNum; ++i)
 
{
  SetThreadPriority(m_pthreads[i], nPriority);
 }

}


int  ThreadPoolModle::GetThreadsPriority()
{
 
return GetThreadPriority(m_pthreads[0]);
}


unsigned 
int  _stdcall ThreadProc(void* lpParam)
{
 ThreadPoolModle
* pThrdPool = static_cast<ThreadPoolModle*>(lpParam);
 
return pThrdPool->Run();
}




--------------------next---------------------

阅读(435) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~