此线程池所依赖的线程类,请参看《一个Windows C++的线程类实现》:
http://blog.csdn.net/huyiyang2010/archive/2010/08/10/5801597.aspx
SystemThreadPool.h
- #define __SYSTEM_THREAD_POOL__
- #include "Thread.h"
- #include <list>
- #include <windows.h>
- class CThreadPoolExecutor
- {
- public:
- CThreadPoolExecutor(void);
- ~CThreadPoolExecutor(void);
- /**
- 初始化线程池,创建minThreads个线程
- **/
- bool Init(unsigned int maxTaskse);
- /**
- 执行任务,若当前任务列表没有满,将此任务插入到任务列表,返回true
- 否则返回false
- **/
- bool Execute(Runnable * pRunnable);
- /**
- 终止线程池,先制止塞入任务,
- 然后等待直到任务列表为空,
- 然后设置最小线程数量为0,
- 等待直到线程数量为空,
- 清空垃圾堆中的任务
- **/
- void Terminate();
- /**
- 返回线程池中当前的线程数量
- **/
- unsigned int GetThreadPoolSize();
- private:
- static unsigned int WINAPI StaticThreadFunc(void * arg);
- private:
- typedef std::list<Runnable *> Tasks;
- typedef Tasks::iterator TasksItr;
- Tasks m_Tasks;
- CRITICAL_SECTION m_csTasksLock;
- volatile bool m_bRun;
- volatile bool m_bEnableInsertTask;
- volatile unsigned int m_maxTasks;
- };
- #endif
SytemThreadPool.cpp
- #include "SystemThreadPool.h"
- CThreadPoolExecutor::CThreadPoolExecutor(void) :
- m_bRun(false),
- m_bEnableInsertTask(false)
- {
- InitializeCriticalSection(&m_csTasksLock);
- }
- CThreadPoolExecutor::~CThreadPoolExecutor(void)
- {
- Terminate();
- DeleteCriticalSection(&m_csTasksLock);
- }
- bool CThreadPoolExecutor::Init(unsigned int maxTasks)
- {
- if(maxTasks == 0)
- {
- return false;
- }
- m_maxTasks = maxTasks;
- m_bRun = true;
- m_bEnableInsertTask = true;
- return true;
- }
- bool CThreadPoolExecutor::Execute(Runnable * pRunnable)
- {
- if(!m_bEnableInsertTask)
- {
- return false;
- }
- if(NULL == pRunnable)
- {
- return false;
- }
- EnterCriticalSection(&m_csTasksLock);
- if(m_Tasks.size() >= m_maxTasks)
- {
- LeaveCriticalSection(&m_csTasksLock);
- return false;
- }
- m_Tasks.push_back(pRunnable);
- LeaveCriticalSection(&m_csTasksLock);
- bool ret = QueueUserWorkItem((LPTHREAD_START_ROUTINE)StaticThreadFunc, this, WT_EXECUTEINPERSISTENTIOTHREAD);
- if(!ret)
- {
- EnterCriticalSection(&m_csTasksLock);
- m_Tasks.remove(pRunnable);
- LeaveCriticalSection(&m_csTasksLock);
- }
- return ret;
- }
- unsigned int CThreadPoolExecutor::GetThreadPoolSize()
- {
- return m_Tasks.size();
- }
- void CThreadPoolExecutor::Terminate()
- {
- m_bEnableInsertTask = false;
- m_bRun = false;
- while(m_Tasks.size() != 0)
- {
- Sleep(1);
- }
- }
- unsigned int WINAPI CThreadPoolExecutor::StaticThreadFunc(void * arg)
- {
- CThreadPoolExecutor * pThreadPool = (CThreadPoolExecutor *)arg;
- Runnable * pRunnable = NULL;
- EnterCriticalSection(&pThreadPool->m_csTasksLock);
- pRunnable = pThreadPool->m_Tasks.front();
- if(NULL != pRunnable)
- {
- pThreadPool->m_Tasks.pop_front();
- }
- LeaveCriticalSection(&pThreadPool->m_csTasksLock);
- if(NULL != pRunnable)
- {
- pRunnable->Run();
- }
- return 0;
- }
用法:
#include "Thread.h"
#include "SystemThreadPool.h"
class R : public Runnable
{
public:
~R()
{
}
void Run()
{
printf("Hello World/n");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CThreadPoolExecutor * pExecutor = new CThreadPoolExecutor();
pExecutor->Init(50);
R r;
for(int i=0;i<100;i++)
{
while(!pExecutor->Execute(&r))
{
}
}
pExecutor->Terminate();
delete pExecutor;
getchar();
return 0;
}
测试结果:
机器:
Intel(R) Core(TM)2 Duo CPU
E8400 @ 3.00GHz
2G内存
对于100个任务并且每个任务包含10000000个循环,任务中无等待:
线程池耗时:2203时间片
from:http://blog.csdn.net/huyiyang2010/article/details/5820548