C/C++ 智能指针线程池

二次信任 提交于 2019-12-05 01:56:58

 

ThreadPool.h

{

#ifndef __THREADPOOL_H__
#define __THREADPOOL_H__
#include <memory>
#include <mutex>
#include <iostream>
#include <thread>
#include <vector>

typedef unsigned int THREADHANDLE;
static THREADHANDLE ThreadHanle = -1;
#define THREADBEGIN(var) if(var == ThreadHanle){return;}

/*
#include "ThreadPool.h"
mutex m;

void func(int threadID)
{
    while (true)
    {
        THREADBEGIN(threadID);
        std::lock_guard<mutex> lg(m);
        static int time = GetTickCount();
        static int index = 0;
        if (GetTickCount() - time >= 1000)
        {
            std::cout << index <<" threadNum: "<< threadID << std::endl;
            time = GetTickCount();
            index++;
        }
    }
}


int Main()
{

    system("color 2");

    ThreadPool<shared_ptr<thread>> T;
    int count = 5;
    for (int i = 0; i < count; i++)
    {
        shared_ptr<thread> ptrT(new thread(func, i));

        T.addChild(std::move(ptrT));
    }

    std::cout << "线程初始完毕!" << std::endl;

    T.stop();

    system("pause");
    return 0;
}
*/

using namespace std;
template<typename PTRTHREAD>
class ThreadPool:public thread
{
    vector<shared_ptr<thread>> *m_vThread = nullptr;
public:
    ThreadPool();
    ThreadPool(PTRTHREAD Thread);
    ~ThreadPool();

    //返回当前这个线程的位置,不是ID
    int addChild(PTRTHREAD Thread);
    //停止一个线程
    void stop(THREADHANDLE ThreadID);
    //停止所有线程
    void stop();
private:
    //void start();
};



template<typename PTRTHREAD>
inline ThreadPool<PTRTHREAD>::ThreadPool()
{
    m_vThread = new vector<shared_ptr<thread>>();
}

//没有join()
template<typename PTRTHREAD>
inline ThreadPool<PTRTHREAD>::ThreadPool(PTRTHREAD Template)
{
    m_vThread = new vector<shared_ptr<thread>>();
    m_vThread->push_back(Template);
    /*
    下面他妈两种方法都可以
    由于继承了thread 所以可以访问get()这个受保护的函数
    */
    //m_vThread[0].begin()->get()->join();
    //m_vThread->begin()->get()->join();
}

template<typename PTRTHREAD>
inline ThreadPool<PTRTHREAD>::~ThreadPool()
{
    if (m_vThread != nullptr)
    {
        for (vector<shared_ptr<thread>>::iterator i = this->m_vThread->begin(); i != this->m_vThread->end(); i++)
        {
            //如果智能指针不为empty
            if ((*i))
            {
                bool ret = i->get()->joinable();
                //如果可以join()
                if (ret)
                {
                    i->get()->join();
                }
                i->reset();
            }
        }
        m_vThread->clear();
        delete m_vThread;
        m_vThread = nullptr;
    }
}

template<typename PTRTHREAD>
int ThreadPool<PTRTHREAD>::addChild(PTRTHREAD ptrThread)
{
    this->m_vThread->push_back(ptrThread);
    int ret = this->m_vThread->size() - 1;
    return ret;
}

template<typename PTRTHREAD>
void ThreadPool<PTRTHREAD>::stop(THREADHANDLE ThreadID)
{
    if (!this->m_vThread->empty())
    {
        int i = 0;
        for (vector<shared_ptr<thread>>::iterator it = this->m_vThread->begin(); it != this->m_vThread->end(); it++,i++)
        {
            if (i == ThreadID)
            {
                if ((*it))
                {
                    ThreadHanle = ThreadID;
                    bool ret = it->get()->joinable();
                    if (ret)
                    {
                        it->get()->join();
                        printf("成功结束线程 <ID = %d> ......\n",ThreadID);
                    }
                    it->reset();
                    it = this->m_vThread->erase(it);
                    //直接return 就不用else
                    return;
                }
            }
            //else
            //{
            //    it++;
            //    i++;
            //}
        }
    }
}

template<typename PTRTHREAD>
void ThreadPool<PTRTHREAD>::stop()
{
    if (!this->m_vThread->empty())
    {
        int i = 0;
        for (vector<shared_ptr<thread>>::iterator it = this->m_vThread->begin(); it != this->m_vThread->end(); )
        {
            if ((*it))
            {
                ThreadHanle = i;
                bool ret = it->get()->joinable();
                if (ret)
                {
                    it->get()->join();
                    printf("成功结束线程 <ID = %d> ......\n", ThreadHanle);
                    it->reset();
                    it = this->m_vThread->erase(it);
                    i++;
                }
                else
                {
                    it++, i++;
                }
            }
        }
    }
}


#endif // !__THREADPOOL_H__

 

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!