C++ 实现简单通用的线程池

来源:互联网 发布:淘宝店铺级别怎么看 编辑:程序博客网 时间:2024/05/23 23:36

利用C++模板特性实现的简单通用线程池

线程池的作用:是为了减少创建和销毁线程所消耗的时间。所以需要事先创建好一定数量的线程,并让这些线程处于等待或挂起状态,等待特定事件的唤醒线程的实现分为3部分 1.异步存取的队列  2.抽象的任务基类提供了一个任务接口 3.线程池
实现://///////////////////////////////////////////////////////////////////////////////////////// 任务队列#include <iostream>#include <list>#include <Windows.h>#include <process.h>template <typename _Ty>class AsynchQueue{public:    AsynchQueue();        ~AsynchQueue();public:    void Edqueue(_Ty* data);    _Ty* Dequeue();private:    HANDLE        m_hEvent;    HANDLE        m_hMutex;    list<_Ty*>    m_queue;    AsynchQueue(const AsynchQueue &);    AsynchQueue& operator= (const AsynchQueue &);};// 抽象的任务类class Task{public:    virtual void DoTask() = 0;};// 线程池class ThreadPool{    public:    ThreadPool(int threadnum,bool release);    ~ThreadPool();private:    static unsigned int __stdcall threadfun(void* lpParam);public:    void Assignment(Task* task);    void Work();    bool IsStopping();private:    int m_iThreadnum;    AsynchQueue<Task> m_queue;    list<HANDLE>    m_hThreads;    HANDLE    m_hMutex;    bool    m_stopMe;    bool    m_releaseTask;};/////////////////////////////////////////////////////////////////////////////////////////// 任务队列 实现部分#include "_threadpool.h"template<typename _Ty>AsynchQueue<_Ty>::AsynchQueue(){    m_hEvent = ::CreateEvent(NULL,FALSE,FALSE,NULL);    m_hMutex = ::CreateMutex(NULL,FALSE,NULL);}template<typename _Ty>AsynchQueue<_Ty>::~AsynchQueue(){    ::CloseHandle(m_hEvent);    ::CloseHandle(m_hMutex);}template<typename _Ty>void AsynchQueue<_Ty>::Edqueue(_Ty * data){    ::WaitForSingleObject(m_hMutex,INFINITE);    m_queue.push_back(data);    ::ReleaseMutex(m_hMutex);    ::SetEvent(m_hEvent);}template<typename _Ty>_Ty* AsynchQueue<_Ty>::Dequeue(){    ::WaitForSingleObject(m_hEvent,INFINITE);    ::WaitForSingleObject(m_hMutex,INFINITE);    _Ty* data = m_queue.front();    m_queue.pop_front();    if(m_queue.size() > 0) ::SetEvent(m_hEvent);    ::ReleaseMutex(m_hMutex);    return data;}// 线程池实现部分ThreadPool::ThreadPool(int threadnum,bool release){        m_iThreadnum = threadnum;    for(int i = 0 ; i < threadnum ; i++)    {        HANDLE hThread;        hThread = (HANDLE)_beginthreadex(NULL,0,threadfun,this,0,NULL);        m_hThreads.push_back(hThread);    }    m_hMutex = ::CreateMutex(NULL,FALSE,NULL);    m_stopMe = false;}ThreadPool::~ThreadPool(){    ::WaitForSingleObject(m_hMutex,INFINITE);    m_stopMe = true;    ::ReleaseMutex(m_hMutex);    HANDLE* handles = new HANDLE[m_iThreadnum];    int i = 0;    list<HANDLE>::iterator item;    for(item = m_hThreads.begin() ; item != m_hThreads.end() ;++item)    {        handles[i++] = *(item);    }    ::WaitForMultipleObjects(m_iThreadnum,handles,TRUE,INFINITE);    // 等待所有线程    for(item = m_hThreads.begin() ; item != m_hThreads.end(); ++item)    {        ::CloseHandle(*item);    }    delete[] handles;        ::CloseHandle(m_hMutex);}void ThreadPool::Assignment(Task* task){    m_queue.Edqueue(task);}bool ThreadPool::IsStopping(){    ::WaitForSingleObject(m_hMutex,INFINITE);    bool stop = m_stopMe;    ::ReleaseMutex(m_hMutex);    return stop;}void ThreadPool::Work(){    Task* task = m_queue.Dequeue();    task->DoTask();    if(m_releaseTask)    {        delete task;    }}unsigned int __stdcall ThreadPool::threadfun(void *lpParam){    ThreadPool* pThis = static_cast<ThreadPool*>(lpParam);    while(!pThis->IsStopping())    {        pThis->Work();    }    return 0;}/////////////////////////////////////////////////////////////////////////////////////////// 测试部分#include "_threadpool.h"#include <string>using namespace std;class TestTask : public Task{public:    TestTask(string sval){m_sval = sval;}    virtual void DoTask()    {        cout << "Excute task :"<< m_sval << endl;    }private:    string m_sval;};int main(int _args,char* _argc){    ThreadPool* pool = new ThreadPool(5,true);    pool->Assignment(new TestTask("hello world"));    pool->Assignment(new TestTask("Simple Thread Pool"));    int nstop; cin >> nstop;}

源码下载

执行结果





原创粉丝点击