#ifndef _THREADPOOLMODLE_H_
#define _THREADPOOLMODLE_H_
#include <cassert>
#include <vector>
using std::vector;
class ThreadPoolModle
{
friend DWORD WINAPI ThreadProc(LPVOID lpParam);
struct ThreadInfo
{
DWORD nThreadID; //线程ID
HANDLE hThisThread; //线程句柄
HANDLE hThreadDeaded; //线程死亡事件
};
bool isPoolEnded;
int m_nNum;
vector<ThreadInfo> m_threads;
HANDLE killAllThreads;
private:
ThreadPoolModle(const ThreadPoolModle& threadModle);
ThreadPoolModle& operator = (const ThreadPoolModle& threadModle);
protected:
LPVOID m_thrdPara;
//线程处理的函数体
virtual void Run() = 0;
void SetThrdDeadOnID(DWORD ThrdID);
bool CheckThrExit();
public:
//默认是只要一个线程的对象
ThreadPoolModle(int num = 1)
{
assert(num >= 1);
m_nNum = num;
m_thrdPara = NULL;
isPoolEnded = false;
killAllThreads = CreateEvent(NULL, true, false, NULL);
}
//得到线程池中的线程数
int ThreadNum()const {return m_nNum;}
//启动线程池所有的线程
bool StartThreads(LPVOID thrdPara);
//停止线程池所有的线程
void EndThreads();
virtual ~ThreadPoolModle(void);
};
#endif
#include "./threadpoolmodle.h"
ThreadPoolModle::~ThreadPoolModle(void)
{
if (!isPoolEnded)
{
EndThreads();
}
}
bool ThreadPoolModle::CheckThrExit()
{
if(WaitForSingleObject(killAllThreads, 0) == WAIT_OBJECT_0)
return true;
else
return false;
}
bool ThreadPoolModle::StartThreads(LPVOID thrdPara)
{
m_thrdPara = thrdPara;
ThreadInfo tmpThrInfo;
for (int i = 0; i < m_nNum; ++i)
{
tmpThrInfo.hThisThread = CreateThread(NULL, 0, ThreadProc, this, 0, &tmpThrInfo.nThreadID);
tmpThrInfo.hThreadDeaded = CreateEvent(NULL, true, false, NULL);
if (tmpThrInfo.hThisThread == NULL || tmpThrInfo.hThreadDeaded == NULL)
{
EndThreads();
return false;
}
m_threads.push_back(tmpThrInfo);
}
/* test
TRACE("m_threads.capacity() = %d/n", m_threads.capacity());
for (int i = 0; i < m_threads.size(); ++i)
{
TRACE("m_threads[%d] = {%d, %d, %d}/n", i , m_threads[i].hThisThread, m_threads[i].nThreadID, m_threads[i].hThreadDead);
}*/
return true;
}
void ThreadPoolModle::EndThreads()
{
int haveExit = 0;
SetEvent(killAllThreads);
while(haveExit < m_nNum)
{
vector<ThreadInfo>::iterator iter = m_threads.begin();
for (;iter != m_threads.end(); ++iter)
{
WaitForSingleObject((*iter).hThreadDeaded, INFINITE);//, 2) == WAIT_OBJECT_0)
++haveExit;
CloseHandle((*iter).hThisThread);
CloseHandle((*iter).hThreadDeaded);
m_threads.erase(iter);
break;
}
}
CloseHandle(killAllThreads);
isPoolEnded = true;
}
void ThreadPoolModle::SetThrdDeadOnID(DWORD ThrdID)
{
vector<ThreadInfo>::iterator iter = m_threads.begin();
for (;iter != m_threads.end(); ++iter)
{
if ((*iter).nThreadID == ThrdID)
{
SetEvent((*iter).hThreadDeaded);
break;
}
}
}
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
ThreadPoolModle* pThrdPool = static_cast<ThreadPoolModle*>(lpParam);
pThrdPool->Run();
pThrdPool->SetThrdDeadOnID(GetCurrentThreadId());
return 0;
}
#define _THREADPOOLMODLE_H_
#include <cassert>
#include <vector>
using std::vector;
class ThreadPoolModle
{
friend DWORD WINAPI ThreadProc(LPVOID lpParam);
struct ThreadInfo
{
DWORD nThreadID; //线程ID
HANDLE hThisThread; //线程句柄
HANDLE hThreadDeaded; //线程死亡事件
};
bool isPoolEnded;
int m_nNum;
vector<ThreadInfo> m_threads;
HANDLE killAllThreads;
private:
ThreadPoolModle(const ThreadPoolModle& threadModle);
ThreadPoolModle& operator = (const ThreadPoolModle& threadModle);
protected:
LPVOID m_thrdPara;
//线程处理的函数体
virtual void Run() = 0;
void SetThrdDeadOnID(DWORD ThrdID);
bool CheckThrExit();
public:
//默认是只要一个线程的对象
ThreadPoolModle(int num = 1)
{
assert(num >= 1);
m_nNum = num;
m_thrdPara = NULL;
isPoolEnded = false;
killAllThreads = CreateEvent(NULL, true, false, NULL);
}
//得到线程池中的线程数
int ThreadNum()const {return m_nNum;}
//启动线程池所有的线程
bool StartThreads(LPVOID thrdPara);
//停止线程池所有的线程
void EndThreads();
virtual ~ThreadPoolModle(void);
};
#endif
#include "./threadpoolmodle.h"
ThreadPoolModle::~ThreadPoolModle(void)
{
if (!isPoolEnded)
{
EndThreads();
}
}
bool ThreadPoolModle::CheckThrExit()
{
if(WaitForSingleObject(killAllThreads, 0) == WAIT_OBJECT_0)
return true;
else
return false;
}
bool ThreadPoolModle::StartThreads(LPVOID thrdPara)
{
m_thrdPara = thrdPara;
ThreadInfo tmpThrInfo;
for (int i = 0; i < m_nNum; ++i)
{
tmpThrInfo.hThisThread = CreateThread(NULL, 0, ThreadProc, this, 0, &tmpThrInfo.nThreadID);
tmpThrInfo.hThreadDeaded = CreateEvent(NULL, true, false, NULL);
if (tmpThrInfo.hThisThread == NULL || tmpThrInfo.hThreadDeaded == NULL)
{
EndThreads();
return false;
}
m_threads.push_back(tmpThrInfo);
}
/* test
TRACE("m_threads.capacity() = %d/n", m_threads.capacity());
for (int i = 0; i < m_threads.size(); ++i)
{
TRACE("m_threads[%d] = {%d, %d, %d}/n", i , m_threads[i].hThisThread, m_threads[i].nThreadID, m_threads[i].hThreadDead);
}*/
return true;
}
void ThreadPoolModle::EndThreads()
{
int haveExit = 0;
SetEvent(killAllThreads);
while(haveExit < m_nNum)
{
vector<ThreadInfo>::iterator iter = m_threads.begin();
for (;iter != m_threads.end(); ++iter)
{
WaitForSingleObject((*iter).hThreadDeaded, INFINITE);//, 2) == WAIT_OBJECT_0)
++haveExit;
CloseHandle((*iter).hThisThread);
CloseHandle((*iter).hThreadDeaded);
m_threads.erase(iter);
break;
}
}
CloseHandle(killAllThreads);
isPoolEnded = true;
}
void ThreadPoolModle::SetThrdDeadOnID(DWORD ThrdID)
{
vector<ThreadInfo>::iterator iter = m_threads.begin();
for (;iter != m_threads.end(); ++iter)
{
if ((*iter).nThreadID == ThrdID)
{
SetEvent((*iter).hThreadDeaded);
break;
}
}
}
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
ThreadPoolModle* pThrdPool = static_cast<ThreadPoolModle*>(lpParam);
pThrdPool->Run();
pThrdPool->SetThrdDeadOnID(GetCurrentThreadId());
return 0;
}