• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

//这个线程池存在一定的BUG 如果没有多线程编程基础的先生请谨慎使用

//我放弃了这种模板方式的线程池,最好不要使用!!!!!!!

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__

 

}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#折半查找法实现代码发布时间:2022-07-13
下一篇:
程序设计入门——C语言期末考试发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap