当前位置:编程学习 > C/C++ >>

c++编写简单线程池

 先上代码:

[cpp] 
#include <iostream> 
#include <vector> 
#include <windows.h> 
using namespace std; 
#define WM_THREADSTART WM_USER+1 
#define WM_THREADEND WM_USER+2 
#define WM_THREADADD  WM_USER+3 
#define  WM_THREADPOOLEND WM_USER+4 
#define  WM_THREADPOOLIDIL WM_USER+5 
class CThreadPool 

public: 
    typedef DWORD (WINAPI *ThreadFunc)(LPVOID); 
    typedef struct _sThread{ 
        HANDLE m_hThread; 
        ThreadFunc m_fFunc; 
        void*    m_vParam; 
        DWORD  m_dRetval; 
    }sThread,*spThread; 
    typedef void (*CallBackFunc)(UINT,spThread); 
    CThreadPool(); 
    ~CThreadPool(); 
    void AddTask(ThreadFunc,LPVOID,bool=0 ); 
    void Init(CallBackFunc,int=10000); 
    DWORD GetRunThreadCount(); 
    spThread GetRunThreadItem(int); 
    DWORD GetWaitThreadCount(); 
    spThread GetWaitThreadItem(int); 
private: 
    static DWORD WINAPI Thread(LPVOID); 
    DWORD m_iThread; 
    HANDLE m_hThread; 
    CallBackFunc m_fFunc; 
    DWORD m_nMaxThreadCount; 
    vector<spThread> m_qRun; 
     
    vector<spThread> m_qWait; 
    HANDLE m_hEvent; 
    CRITICAL_SECTION m_cs; 
}; 
CThreadPool::CThreadPool() 

    m_iThread=0; 
    m_fFunc=0; 
    m_hThread=0; 
    InitializeCriticalSection(&m_cs); 

CThreadPool::~CThreadPool() 

    PostThreadMessage(m_iThread,WM_THREADPOOLEND,0,0); 
    WaitForSingleObject(m_hThread,-1); 
    CloseHandle(m_hThread); 
    LeaveCriticalSection(&m_cs); 
    int i; 
    for(i=0;i<m_qRun.size();i++) 
    { 
        delete m_qRun[i]; 
    } 
    for(i=0;i<m_qWait.size();i++) 
    { 
        delete m_qWait[i]; 
    } 

void CThreadPool::AddTask(ThreadFunc func,LPVOID lp,bool bImme) 

    if(!bImme) 
    { 
        PostThreadMessage(m_iThread,WM_THREADADD,(WPARAM)func,(LPARAM)lp); 
    } 
    else 
    { 
        spThread sTh=new sThread; 
        sTh->m_dRetval=0; 
        sTh->m_hThread=0; 
        sTh->m_fFunc=func; 
        sTh->m_vParam=lp; 
        EnterCriticalSection(&m_cs); 
        m_qWait.insert(m_qWait.begin(),sTh); 
        LeaveCriticalSection(&m_cs); 
    } 
    PostThreadMessage(m_iThread,WM_THREADPOOLIDIL,0,0); 
 

void CThreadPool::Init(CallBackFunc func,int count/* =10000 */) 

    if(func!=NULL)  m_fFunc=func; 
    if(count>0)    m_nMaxThreadCount=count; 
    m_hEvent=CreateEvent(0,TRUE,FALSE,0); 
    m_hThread=CreateThread(0,0,Thread,this,0,&m_iThread); 
    WaitForSingleObject(m_hEvent,-1); 
    CloseHandle(m_hEvent); 

DWORD WINAPI CThreadPool::Thread(LPVOID lp) 

    CThreadPool* pThPool=(CThreadPool*)lp; 
    DWORD dwCurId=GetCurrentThreadId(); 
    MSG msg; 
    PeekMessage(&msg,0,WM_USER,WM_USER,PM_NOREMOVE); 
    SetEvent(pThPool->m_hEvent); 
    while (1) 
    { 
        Sleep(1); 
         
        if(PeekMessage(&msg,0,WM_THREADSTART,WM_THREADPOOLIDIL,PM_REMOVE)) 
        { 
             
            switch (msg.message) 
            { 
            case WM_THREADPOOLEND: 
                { 
                    return true; 
                } 
            case WM_THREADSTART: 
                { 
                    spThread sTh=(spThread)msg.wParam; 
                    sTh->m_hThread=CreateThread(0,0,sTh->m_fFunc,sTh->m_vParam,0,0); 
                    if(pThPool->m_fFunc)  pThPool->m_fFunc(WM_THREADSTART,sTh); 
                    break; 
                } 
            case WM_THREADEND: 
                { 
                    spThread sTh=(spThread)msg.wParam; 
                    if(pThPool->m_fFunc)  pThPool->m_fFunc(WM_THREADEND,sTh); 
        &

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,