此线程池所依赖的线程类,请参看《一个Windows C++的线程类实现》:
http://blog.csdn.net/huyiyang2010/archive/2010/08/10/5801597.aspx
ThreadPoolExecutor.h
ThreadPoolExecutor.cpp
用法:
#include "Thread.h"#include "ThreadPoolExecutor.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(1, 10, 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个循环,任务中无等待:
单线程执行耗时:2281时间片
单线程池执行耗时:2219时间片
2个线程的线程池耗时:1156时间片
5个线程的线程池耗时:1166时间片
10个线程的线程池耗时:1157时间片
100个线程的线程池耗时:1177时间片
from: