简易线程池手撕实现

version1.0

第一版,没有实现动态扩容,简单实现多线程取得任务和执行

header.h

存储所有需要用到的系统头文件,防止重复定义和引用

#pragma once
#include <iostream>
#include <condition_variable>
#include <mutex>
#include <atomic>
#include <thread>
#include <memory>
#include <vector>
#include <map>
#include <functional>
#include <queue>

threadpool.h

#pragma once
#include "header.h"

class threadpool
{
public:
    threadpool(int min,int max);
    ~threadpool(); //最后确定需要销毁的资源
    void addTask(std::function<void(void)> f);
private:
    void manager();  //管理者线程
    void worker();   //工作线程
    void shotdown();
private:
    std::thread* m_manager;
    std::vector<std::thread> v_threadIds;  //记录需要销毁的线程数量
    std::map<std::thread::id,std::thread> m_TidToThread;
    std::atomic<int> m_minNums;   //最小可用线程数
    std::atomic<int> m_maxNums;   //最大可用线程数
    std::atomic<int> m_curNums;   //现在正在使用线程数
    std::atomic<int> m_idleNums;  //现在空闲线程数
    std::queue<std::function<void(void)>> m_tasks;  //任务队列
    std::mutex m_queueMtx;  //任务队列共享资源加锁使用
    std::condition_variable m_condition;  //线程条件变量
    std::atomic<bool> m_stop;  //判断当前线程池是否需要停止
};

threadpool.cpp

#include "../include/threadpool.h"

threadpool::threadpool(int min, int max)
    : m_minNums(min), m_maxNums(max), m_stop(false), m_curNums(0), m_idleNums(0)
{
    m_manager = new std::thread(&threadpool::manager, this); // 创造线程,运行当前实例之中的m_manager线程
    m_curNums = m_idleNums = min;
    for (int i = 0; i < m_minNums; ++i)
    {
        std::thread t(&threadpool::worker, this); // 创建一个临时对象
        m_TidToThread.insert(make_pair(t.get_id(), move(t)));
    }
}

void threadpool::addTask(std::function<void(void)> f)
{
    // queue不会满,只会为空,遇到任务,直接加到queue中,但要加锁
    {
        std::lock_guard<std::mutex> lck(m_queueMtx);
        m_tasks.emplace(f);
    }
    m_condition.notify_one(); //通知其他线程去取任务
}

void threadpool::manager()
{
    // 暂时不考虑动态变化线程
    while (!m_stop.load())
    {
    }
}

void threadpool::worker()
{
    while (!m_stop.load())
    {
        std::function<void(void)> f = nullptr;
        {
            std::unique_lock<std::mutex> locker(m_queueMtx);
            while (!m_stop && m_tasks.empty())
            {
                m_condition.wait(locker);
            }
            if(!m_tasks.empty())
            {
                std::cout << " 取出一个任务 " << std::endl;
                f = move(m_tasks.front());
                m_tasks.pop();
            }
        }
        if(f)
        {
            m_idleNums--;
            f();
            m_idleNums++;
        }
    }
}

void threadpool::shotdown()
{
    m_stop = true;
    m_condition.notify_all();
    for(auto& it :m_TidToThread)
    {
        auto & t = it.second;
        if(t.joinable())
        {
            std::cout << "线程将被销毁:" << t.get_id() << "\n";
            t.join();
        }
    }
    if(m_manager->joinable())
    {
        m_manager->join();
    }
    delete m_manager;
}

threadpool::~threadpool() // 最后确定需要销毁的资源
{
    shotdown();
}

void calc(int x, int y)
{
    int res = x + y;
    std::cout << "res = " << res << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
}

int main()
{
    threadpool pool(2,4);
    for (int i = 0; i < 10; ++i)
    {
        auto func = std::bind(calc, i, i * 2);
        pool.addTask(func);
    }
    getchar();
    return 0;
}

git 训练

版本一上传git

git checkout -b version1.0 

git add .

git commit -m "第一次提交版本1.0"

git push origin version1.0

合并到master分支

git checkout master 切回master分支

git merge version1.0 将1.0合并入master分支

git push origin master 合并

ps:已经更新过多版本,记得更新前线git pull origin master 拉去最新版本,否则落后commit同事领先commit会有冲突产生

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值