多线程消息处理(支持动态调整线程数)

MultiThreadEx.h

cpp 复制代码
#pragma once

#include <thread>
#include <map>
#include <list>
#include <queue>
#include <mutex>
#include <memory>
#include <condition_variable>
#include <atomic>

using TaskFun = std::function<void(void)>;

class CMultiThreadEx {
public:
    CMultiThreadEx();
    ~CMultiThreadEx();

    bool start();

    void stop();

    bool post(const TaskFun& task_fun);

private:
    void exec();
    void add_thread();
    void del_thread();
    void del_thread_oper();
private:

    std::atomic_bool exist_;
    std::mutex thread_mutex_;
    std::map<std::thread::id, std::shared_ptr<std::thread>> threads_;
    std::list<std::thread::id> threads_need_del_;
    std::atomic<int32_t> thread_num_;
    std::atomic<int32_t> task_num_;
    std::queue<TaskFun> tasks_;
    std::mutex task_mutex_;
    std::condition_variable condition_;

    int32_t min_thead_num_;
    int32_t max_thead_num_;
    int32_t task_avg_num_;
};

MultiThreadEx.cpp:

cpp 复制代码
#include "MultiThreadEx.h"
#include <iostream>

CMultiThreadEx::CMultiThreadEx()
    : exist_(false)
    , min_thead_num_(1)
    , max_thead_num_(1024)
    , task_avg_num_(10)
{
}

CMultiThreadEx::~CMultiThreadEx() {}

bool CMultiThreadEx::start() {
    if (exist_) {
        return false;
    }

    exist_ = true;

    for (size_t i = 0; i < min_thead_num_; i++)
    {
        add_thread();
    }

    return true;
}

void CMultiThreadEx::stop() {
    if (!exist_) {
        return;
    }

    exist_ = false;
    condition_.notify_all();

    for (auto& iter_thread : threads_)
    {
        if (iter_thread.second->joinable()) {
            iter_thread.second->join();
        }
    }

    return;
}

bool CMultiThreadEx::post(const TaskFun& task_fun) {
    if (!exist_) {
        return false;
    }

    {
        std::lock_guard<std::mutex> lock(task_mutex_);
        tasks_.push(task_fun);
        ++task_num_;
    }

    if (thread_num_ < min_thead_num_
        || task_num_ > thread_num_ * task_avg_num_)
    {
        add_thread();
    }

    condition_.notify_one();
    return true;
}

void CMultiThreadEx::exec() {
    while (exist_)
    {
        TaskFun task = nullptr;
        del_thread_oper();

        {
            std::unique_lock<std::mutex> lock(task_mutex_);
            if (tasks_.empty()) {

                if (thread_num_ > min_thead_num_) {

                    auto ret =condition_.wait_for(lock, std::chrono::milliseconds(1000));
                    if (std::cv_status::timeout == ret)
                    {
                        // 退出线程
                        del_thread();
                        break;
                    }

                    if (!exist_) {
                        break;
                    }

                }

                condition_.wait(lock);
            }

            if (!exist_) {
                break;
            }

            if (!tasks_.empty())
            {
                task = tasks_.front();
                tasks_.pop();
                --task_num_;
            }
        }

        if (nullptr != task) {
            task();
        }
    }

    return;
}

void CMultiThreadEx::add_thread() {
    std::lock_guard<std::mutex> lock(thread_mutex_);

    std::shared_ptr<std::thread> sptr_thread =
        std::make_shared<std::thread>(std::thread(std::bind(&CMultiThreadEx::exec, this)));
    threads_.insert(std::make_pair(sptr_thread->get_id(), sptr_thread));
    ++thread_num_;
}

void CMultiThreadEx::del_thread(){
    std::lock_guard<std::mutex> lock(thread_mutex_);

    auto id = std::this_thread::get_id();
    auto iter_thread = threads_.find(id);

    if (threads_.end() != iter_thread) {
        threads_need_del_.push_back(id);
        --thread_num_;
    }
}

void CMultiThreadEx::del_thread_oper(){
    std::lock_guard<std::mutex> lock(thread_mutex_);

    if (threads_need_del_.empty())
    {
        return;
    }

    for (auto& id : threads_need_del_)
    {
        auto iter_thread = threads_.find(id);

        if (threads_.end() != iter_thread){
            if (iter_thread->second->joinable()) {
                iter_thread->second->join();
            }
            threads_.erase(id);
        }
    }
}

main:

cpp 复制代码
#include <iostream>
#include "MultiThreadEx.h"

int main() {

    std::mutex m;

    CMultiThreadEx t;
    t.start();

    auto fun = [&]()
    {
        std::lock_guard<std::mutex> lock(m);

        static int count = 0;
        std::cout << "Count: " << ++count << "thread-id: " << std::this_thread::get_id() << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    };

    for (int i = 0; i < 20; ++i)
    {
        t.post(fun);
        //std::this_thread::sleep_for(std::chrono::microseconds(500));
    }

    getchar();
    t.post(fun);
    getchar();
    t.stop();

    return 0;
}
相关推荐
Jtti2 小时前
Windows系统服务器怎么设置远程连接?详细步骤
运维·服务器·windows
小奥超人3 小时前
PPT文件设置了修改权限,如何取消权?
windows·经验分享·microsoft·ppt·办公技巧
hairenjing112312 小时前
使用 Mac 数据恢复从 iPhoto 图库中恢复照片
windows·stm32·嵌入式硬件·macos·word
九鼎科技-Leo14 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo16 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
黎明晓月20 小时前
Java之字符串分割转换List
java·windows·list
九鼎科技-Leo21 小时前
在 C# 中,ICollection 和 IList 接口有什么区别?
windows·c#·.net
顾辰呀21 小时前
实现uniapp-微信小程序 搜索框+上拉加载+下拉刷新
前端·windows
Bunny Chen1 天前
如何缩小PPT演示文稿的大小?
windows·microsoft·powerpoint
如光照1 天前
Linux与Windows中的流量抓取工具:wireshark与tcpdump
linux·windows·测试工具·网络安全