码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Linux-线程池


    文章目录

    • 前言
    • 一、线程池是什么?
    • 二、示例代码


    前言

    线程池主要是对之前内容的一个巩固,并且初步了解池化概念。


    一、线程池是什么?

    线程池就是提前开辟好一块空间,随时准备创造新线程来完成任务,可以理解为用空间来换时间,具体实现看以下示例代码。

    二、示例代码

    #include 
    #include 
    #include 
    #include "lockGuard.hpp"
    #include "log.hpp"
    const int default_ThreadNum = 5;
    template <class T>
    class ThreadPool
    {
    
    public:
        ThreadPool(int thread_num = default_ThreadNum)
        :_thread_num(thread_num)
        {
            pthread_mutex_init(&_mutex,nullptr);
            pthread_cond_init(&_cond,nullptr);
            for (int i = 1; i <= _thread_num; i++)
            {
                char nameBuffer[128];
                snprintf(nameBuffer, sizeof nameBuffer, "Thread %d", i);
                _threadPool.push_back(new Thread(nameBuffer, routine, (void *)this));
                logMessage(NORMAL, "%s 线程创建成功!", nameBuffer);
            }
        }
    
        bool isEmpty()
        {
            return _task_queue.empty();
        }
    
        void waitCond()
        {
            pthread_cond_wait(&_cond, &_mutex);
        }
    
        pthread_mutex_t &getMutex()
        {
            return _mutex;
        }
    
    
        T getTask()
        {
            T task = _task_queue.front();
            _task_queue.pop();
            return task;
        }
    
        std::vector<Thread> &getpool()
        {
            return _threadPool;
        }
    
        static void *routine(void *args)
        {
            ThreadData *td = (ThreadData *)args;
            ThreadPool<T> *tp = (ThreadPool<T> *)td->_args;
            while (1)
            {
                T task;
                {
                    lockGuard lg(&tp->getMutex());
                    while (tp->isEmpty())
                        tp->waitCond();
                    task = tp->getTask();
                }
                task(td->_name);
            }
        }
    
        void run()
        {
            for(auto& thread : _threadPool)
            {
                thread->start();
            }
        }
    
         void pushTask(const T &task)
         {
            lockGuard lg(&_mutex);
            _task_queue.push(task);
            pthread_cond_signal(&_cond);
         }
    
        ~ThreadPool()
        {
            for(auto& iter: _threadPool)
            {
                iter->join();
                delete iter;
            }
            pthread_mutex_destroy(&_mutex);
            pthread_cond_destroy(&_cond);
        }
    
    private:
        int _thread_num;
        std::vector<Thread*> _threadPool;
        std::queue<T> _task_queue;
    
        pthread_mutex_t _mutex;
        pthread_cond_t _cond;
    };
    

  • 相关阅读:
    java计算机毕业设计西安市城市绿地管理系统源程序+mysql+系统+lw文档+远程调试
    19 C++设计模式之中介者(Mediator)模式
    SpringMVC 04: SpringMVC中4种页面跳转方式
    【数据结构】08排序
    【开课预告】下半年软考(中/高级)认证将于8月20日开班
    算法宝典——Java版本(持续更新)
    【杂记-浅谈OSPF协议中的Router ID】
    ubuntu循环登录,无法进入桌面
    js中super的使用
    基于SpringBoot的校园社团信息管理系统
  • 原文地址:https://blog.csdn.net/fengjunziya/article/details/139034156
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号