使用QueueUserWorkerItem实现的线程池封装

此线程池所依赖的线程类,请参看《一个Windows C++的线程类实现》:

http://blog.csdn.net/huyiyang2010/archive/2010/08/10/5801597.aspx

 

SystemThreadPool.h

 

  1. #define __SYSTEM_THREAD_POOL__  
  2. #include "Thread.h"  
  3. #include <list>  
  4. #include <windows.h>  
  5. class CThreadPoolExecutor  
  6. {  
  7. public:  
  8.     CThreadPoolExecutor(void);  
  9.     ~CThreadPoolExecutor(void);  
  10.     /** 
  11.       初始化线程池,创建minThreads个线程 
  12.     **/  
  13.     bool Init(unsigned int maxTaskse);  
  14.     /** 
  15.       执行任务,若当前任务列表没有满,将此任务插入到任务列表,返回true 
  16.       否则返回false 
  17.     **/  
  18.     bool Execute(Runnable * pRunnable);  
  19.     /** 
  20.       终止线程池,先制止塞入任务, 
  21.       然后等待直到任务列表为空, 
  22.       然后设置最小线程数量为0, 
  23.       等待直到线程数量为空, 
  24.       清空垃圾堆中的任务 
  25.     **/  
  26.     void Terminate();  
  27.     /** 
  28.       返回线程池中当前的线程数量 
  29.     **/  
  30.     unsigned int GetThreadPoolSize();  
  31. private:  
  32.     static unsigned int WINAPI StaticThreadFunc(void * arg);  
  33. private:  
  34.     typedef std::list<Runnable *> Tasks;  
  35.     typedef Tasks::iterator TasksItr;  
  36.     Tasks m_Tasks;  
  37.     CRITICAL_SECTION m_csTasksLock;  
  38.     volatile bool m_bRun;  
  39.     volatile bool m_bEnableInsertTask;  
  40.     volatile unsigned int m_maxTasks;  
  41. };  
  42. #endif  

 

SytemThreadPool.cpp

 

  1. #include "SystemThreadPool.h"  
  2. CThreadPoolExecutor::CThreadPoolExecutor(void) :   
  3. m_bRun(false),  
  4. m_bEnableInsertTask(false)  
  5. {  
  6.     InitializeCriticalSection(&m_csTasksLock);  
  7. }  
  8. CThreadPoolExecutor::~CThreadPoolExecutor(void)  
  9. {  
  10.     Terminate();  
  11.     DeleteCriticalSection(&m_csTasksLock);  
  12. }  
  13. bool CThreadPoolExecutor::Init(unsigned int maxTasks)  
  14. {  
  15.     if(maxTasks == 0)  
  16.     {  
  17.         return false;  
  18.     }  
  19.     m_maxTasks = maxTasks;  
  20.     m_bRun = true;  
  21.     m_bEnableInsertTask = true;  
  22.     return true;  
  23. }  
  24. bool CThreadPoolExecutor::Execute(Runnable * pRunnable)  
  25. {  
  26.     if(!m_bEnableInsertTask)  
  27.     {  
  28.         return false;  
  29.     }  
  30.     if(NULL == pRunnable)  
  31.     {  
  32.         return false;  
  33.     }  
  34.     EnterCriticalSection(&m_csTasksLock);  
  35.     if(m_Tasks.size() >= m_maxTasks)  
  36.     {  
  37.         LeaveCriticalSection(&m_csTasksLock);  
  38.         return false;  
  39.     }  
  40.     m_Tasks.push_back(pRunnable);  
  41.     LeaveCriticalSection(&m_csTasksLock);  
  42.     bool ret = QueueUserWorkItem((LPTHREAD_START_ROUTINE)StaticThreadFunc, this, WT_EXECUTEINPERSISTENTIOTHREAD);  
  43.     if(!ret)  
  44.     {  
  45.         EnterCriticalSection(&m_csTasksLock);  
  46.         m_Tasks.remove(pRunnable);  
  47.         LeaveCriticalSection(&m_csTasksLock);  
  48.     }  
  49.     return ret;  
  50. }  
  51. unsigned int CThreadPoolExecutor::GetThreadPoolSize()  
  52. {  
  53.     return m_Tasks.size();  
  54. }  
  55. void CThreadPoolExecutor::Terminate()  
  56. {  
  57.     m_bEnableInsertTask = false;  
  58.     m_bRun = false;  
  59.     while(m_Tasks.size() != 0)  
  60.     {  
  61.         Sleep(1);  
  62.     }  
  63. }  
  64. unsigned int WINAPI CThreadPoolExecutor::StaticThreadFunc(void * arg)  
  65. {  
  66.     CThreadPoolExecutor * pThreadPool = (CThreadPoolExecutor *)arg;  
  67.     Runnable * pRunnable = NULL;  
  68.     EnterCriticalSection(&pThreadPool->m_csTasksLock);  
  69.     pRunnable = pThreadPool->m_Tasks.front();  
  70.     if(NULL != pRunnable)  
  71.     {  
  72.         pThreadPool->m_Tasks.pop_front();  
  73.     }  
  74.     LeaveCriticalSection(&pThreadPool->m_csTasksLock);  
  75.     if(NULL != pRunnable)  
  76.     {  
  77.         pRunnable->Run();  
  78.     }  
  79.     return 0;  
  80. }  

 

用法:

 

#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

原文地址:https://www.cnblogs.com/lidabo/p/3328649.html