线程池的一个例子
ThreadPool.h:
// ThreadPool.h: interface for the CThreadPool class.
//
//
#if !defined(AFX_THREADPOOL_H__E4160016_0FD0_4E25_A708_A3240945C9B9__INCLUDED_)
#define AFX_THREADPOOL_H__E4160016_0FD0_4E25_A708_A3240945C9B9__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//线程池类 BY : SinCoder 不要改版权 否则 程序将会有改不完的 BUG !!!
#include <windows.h>
#define MAX_THREAD 1000 //最大的线程数 。。
typedef void (CALLBACK *_ProcCallBack)(LPVOID );
class CThreadPool
{
public:
CThreadPool();
virtual ~CThreadPool();
public:
int ThreadPoolInit(UINT threadnum); //初始化线程池 返回线程池创建线程的数量
int PostJob(_ProcCallBack _proc,LPVOID lparam); //提交任务 返回活动线程的数量
int StopThreadPool();
private:
static DWORD WINAPI threadProc(LPVOID lparam); //线程函数
private:
HANDLE hEvent ;//= CreateEvent(NULL,FALSE,FALSE,NULL); 通知任务的事件
UINT dwThread; //线程计数
HANDLE JobEvent;
LPVOID JobLparam; //任务的参数
BOOL Exitthread;//=FALSE; //线程函数退出标示
UINT WorkingThread;//=0; //活动线程计数
_ProcCallBack JobProc;//=NULL; //函数地址
BOOL IsPoolStart;//=FALSE; //线程池是否启动了
};
#endif // !defined(AFX_THREADPOOL_H__E4160016_0FD0_4E25_A708_A3240945C9B9__INCLUDED_)
ThreadPool.c
// ThreadPool.cpp: implementation of the CThreadPool class.
//
//
#include "ThreadPool.h"
#include <stdio.h>
//
// Construction/Destruction
//
CThreadPool::CThreadPool()
{
hEvent =NULL;//CreateEvent(NULL,FALSE,FALSE,NULL); //通知任务的事件
dwThread = 0; //
JobLparam = NULL; //任务的参数
JobProc =NULL; //函数地址
JobEvent = NULL;// 任务的互斥。。
Exitthread = FALSE; //线程函数退出标示
WorkingThread =0; //活动线程计数
IsPoolStart =FALSE;
}
CThreadPool::~CThreadPool()
{
}
int CThreadPool::ThreadPoolInit(UINT threadnum)
{
if(threadnum <= 0 || threadnum > MAX_THREAD)
return 0;
hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
JobEvent = CreateEventA(NULL,FALSE,TRUE,NULL);
for(DWORD idx=0;idx < threadnum;idx++)
{
HANDLE hth = CreateThread(NULL,0,threadProc,this,0,NULL);
if(hth)
{
CloseHandle(hth);
dwThread ++ ;
}
}
IsPoolStart = TRUE;
return dwThread;
}
DWORD WINAPI CThreadPool::threadProc(LPVOID lparam)
{
CThreadPool *pt = (CThreadPool *)lparam;
while(!pt->Exitthread)
{
_ProcCallBack MyProc = NULL;
LPVOID MyLparam = NULL;
int err = WaitForSingleObject(pt->hEvent,100);
if(pt->Exitthread)
return 0;
switch(err)
{
case WAIT_TIMEOUT:
{
// printf("Wait time out\r\n");
}
break;
case WAIT_OBJECT_0:
{
InterlockedExchangeAdd((long *)&pt->WorkingThread,1);
MyProc = pt->JobProc;
MyLparam = pt->JobLparam;
SetEvent(pt->JobEvent);
if(MyProc)
MyProc(MyLparam);
InterlockedExchangeAdd((long *)&pt->WorkingThread,-1);
}
break;
default: //出错了。。。
return 0;
}
}
return 0;
}
int CThreadPool::PostJob(_ProcCallBack _proc,LPVOID lparam)
{
if(!IsPoolStart || _proc == NULL)
return 0;
while(WorkingThread >= dwThread) //如果没有空闲的线程。。。
{
Sleep(100);
}
WaitForSingleObject(JobEvent,INFINITE);
JobProc = _proc;
JobLparam = lparam;
SetEvent(hEvent); //有任务了。。
return WorkingThread;
}
int CThreadPool::StopThreadPool()
{
Sleep(1000);
Exitthread=TRUE;
while(WorkingThread > 0)
Sleep(100);
dwThread = 0;
IsPoolStart = FALSE;
WorkingThread = 0;
JobProc = NULL;
JobLparam=NULL;
CloseHandle(hEvent);
CloseHandle(JobEvent);
return TRUE;
}