• Linux多线程篇【5】——线程池


    线程池

    什么是线程池?在学STL接口的时候我们发现很多的结构都是有自动的扩容机制的,但频繁的扩容会有一定的代价,就好比我每次需要一块钱的时候每次都只向爸爸要一块钱,而爸爸在距离我500m远的距离,那么当我总共需要10块钱的时候就要来回跑10趟,十分耗时间,那么我不如直接一次性跟他要10块钱,就算有多的我也就先放在身上,有需要的时候就可以直接使用。内存池就是一次性去跟系统申请一大块内存空间来减少申请内存的次数。

    与内存池类似,创建线程也是需要一定的代价的,因此我们也可以采用一次性申请多个线程的方式来提高效率。我们接下来的代码希望能够实现如下的功能

    1. 创建多个线程
    2. 将任务放入任务队列
    3. 线程以竞争的方式来获取任务队列中的任务,并执行任务

    thread_pool.hpp

    namespace ssj_thread_pool
    {
        const int g_num = 5;
        template<class T>
        class ThreadPool
        {
        private:
            int _num;
            std::queue<T> _task_queue;
    
            pthread_mutex_t _mtx;
            pthread_cond_t _cond;
        public:
        // 将lock,unlock,wait,wakeup,isempty进行封装不但使用
        // 方便而且因为Routine是静态的,所以无法访问类内私有成员(mtx
        // ,cond,taskqueue)将他们封装可以解决这个问题。
            void Lock()
            {
                pthread_mutex_lock(&_mtx);
            }
            void Unlock()
            {
                pthread_mutex_unlock(&_mtx);
            }
            void Wait()
            {
                pthread_cond_wait(&_cond, &_mtx);
            }
            void WakeUp()
            {
                pthread_cond_signal(&_cond);
            }
            bool IsEmpty()
            {
                return _task_queue.empty();
            }
            // 在类内的成员函数会被默认传递this指针,因此需要使用静态成员函数
            static void* Routine(void *args)
            {
            	// 线程分离,无需再进行等待
                pthread_detach(pthread_self());
                ThreadPool<T> *tp = (ThreadPool<T>*)args;
    
                while (true)
                {
                    tp->Lock();
                    // 使用循环判断防止伪唤醒
                    while (tp->IsEmpty())
                    {
                        tp->Wait();
                    }
                    T t;
                    tp->PopTask(&t);
                    tp->Unlock();
    				// 将执行任务放在锁外进行,这样可以让其
    				// 它线程在任务执行时继续从任务队列读取
    				// 任务达到多任务并发执行的效果
                    t();
                }
            }
        public:
            void InitThreadPool()
            {
                for (int i = 0; i < _num; i++)
                {
                    pthread_t tid;
                    /* 将对象地址传给Routine,因为Routine是
                    静态成员函数,没有this指针,所以不能访问类
                    内成员,因此将this指针传给它*/
                    pthread_create(&tid, nullptr, Routine, (void*)this);
                    sleep(1);
                }
            }
            void PushTask(const T &in)
            {
                Lock();
                _task_queue.push(in);
                Unlock();
                WakeUp();
            }
            void PopTask(T *out)
            {
                *out = _task_queue.front();
                _task_queue.pop();
            }
            ThreadPool(int num = g_num)
            : _num(num)
            {
                pthread_mutex_init(&_mtx, nullptr);
                pthread_cond_init(&_cond, nullptr);
            }
            ~ThreadPool()
            {
                pthread_mutex_destroy(&_mtx);
                pthread_cond_destroy(&_cond);
            }
        };
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98

    任务

    我们创建的任务就是用x和y来执行op对应的±*/%运算

    namespace ssj_task
    {
        class Task
        {
        private:
            int _x;
            int _y;
            char _op;
        public:
            Task() {}
            Task(int x, int y, char op)
            : _x(x)
            , _y(y)
            , _op(op)
            {}
            std::string Show()
            {
                std::string message = std::to_string(_x);
                message += _op;
                message += std::to_string(_y);
                message += "=?";
                return message;
            }
            int Run()
            {
                int res = 0;
                 switch(_op)
                 {
                    case '+':
                        res = _x + _y;
                        break;
                    case '-':
                        res = _x - _y;
                        break;
                    case '*':
                        res = _x * _y;
                        break;
                    case '/':
                        res = _x / _y;
                        break;
                    case '%':
                        res = _x % _y;
                        break;
                    default:
                        std::cout << "Please choose another option" << std::endl;
                        break;
                 }
                 std::cout << "当前任务正在被:" << pthread_self() << "处理" \
                 << _x << _op << _y << "=" << res << std::endl;
                 return res;
            }
            int operator()()
            {
                return Run();
            }
            ~Task() {}
        };
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    main.cc

    int main()
    {
        ThreadPool<Task> *tp = new ThreadPool<Task>();
        tp->InitThreadPool();
        srand((long long)time(nullptr));
        while(true)
        {
            Task t(rand() % 20 + 1, rand() % 10 + 1, "+-*/%"[rand() % 5]);
            tp->PushTask(t);
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    单例模式

    单例模式是一种经典的设计模式。对于某些类只应该具有一个对象,就称之为单例。我们定义对象时实际上需要两个步骤:

    • 1.开辟空间
    • 2.给空间写入初始值
      这两个步骤本质上就是把对象加载入内存,而单例模式就是只让该对象在内存中存在一份。一般而言需要被设计成单例模式的对象主要有如下的特征:
    • 语义上只需要一个对象。(只需要一个对象)
    • 该对象内部存在大量的空间,保存了大量的数据,如果允许该对象存在多份,或者允许发生各种拷贝,内存中会存在冗余数据。
      那么我们应该选择在什么时候将对象加载入内存呢?我们又对此区分了饿汉方式和懒汉方式。

    什么是设计模式

    设计模式可以看作是一种经验,根据一些经典的场景,给定了一些特定的解决方案,这些解决方案就是设计模式

    饿汉方式和懒汉方式

    就像不同的同学写暑假作业会选择在不同的时间一样,有的同学可能会 在领到暑假作业后前几天就把暑假作业完成了,这种方式就称为饿汉方式,而有的同学心理承受能力极强,对于堆积如山的作业嗤之以鼻,相信自己能够在开学之前的那个晚上把他们轻松完成,这种方式就称为懒汉方式。不难看出对于第一类同学来说,他们在假期开始的前几天可能会比较痛苦,但是后面就会玩的很开心,而第二类同学则恰好相反,在假期开始时他们可以玩的很爽,但是在最后他们也会经历补作业的痛苦。

    懒汉方式

    懒汉方式最核心的思想是“延时加载”,从而能够优化服务器的启动速度。写时拷贝就是懒汉方式的一个例子。
    我们之前写的线程池其实就是一个单例,因为我们只需要一个线程池就够了,如果我们需要更多的线程,那么直接创建更多的线程就好了。因此接下来我们就试着用懒汉方式来修改一下我们线程池的代码。

    单例模式线程池

    1. 将构造函数赋值语句等设为私有
    2. 在类外初始化对象指针
    3. 获取对象函数
    namespace ssj_thread_pool
    {
        const int g_num = 5;
        template <class T>
        class ThreadPool
        {
        private:
            int _num;
            std::queue<T> _task_queue;
    
            pthread_mutex_t _mtx;
            pthread_cond_t _cond;
            static ThreadPool<T> *_ins;
    
        private:
            // 构造函数必须实现,且必须私有
            ThreadPool(int num = g_num)
                : _num(num)
            {
                pthread_mutex_init(&_mtx, nullptr);
                pthread_cond_init(&_cond, nullptr);
            }
    
            ThreadPool(const ThreadPool<T> &tp) = delete;
    
            ThreadPool<T> &operator=(ThreadPool<T> &tp) = delete;
    
        public:
            void Lock()
            {
                pthread_mutex_lock(&_mtx);
            }
            void Unlock()
            {
                pthread_mutex_unlock(&_mtx);
            }
            void Wait()
            {
                pthread_cond_wait(&_cond, &_mtx);
            }
            void WakeUp()
            {
                pthread_cond_signal(&_cond);
            }
            bool IsEmpty()
            {
                return _task_queue.empty();
            }
            static void *Routine(void *args) // 在类内的成员函数会被默认传递this指针,因此需要使用静态成员函数
            {
                pthread_detach(pthread_self());
                ThreadPool<T> *tp = (ThreadPool<T> *)args;
    
                while (true)
                {
                    tp->Lock();
                    while (tp->IsEmpty())
                    {
                        tp->Wait();
                    }
                    T t;
                    tp->PopTask(&t);
                    tp->Unlock();
    
                    t();
                }
            }
    
        public:
            static ThreadPool<T> *GetInstance()
            {
                static pthread_mutex_t _lock = PTHREAD_MUTEX_INITIALIZER;
                // 当前单例对象还没有被创建
                if (_ins == nullptr) // 申请锁的代价较高,因此使用双判断,减少锁的征用
                {
                    pthread_mutex_lock(&_lock);
                    if (_ins == nullptr)
                    {
                        _ins = new ThreadPool<T>();
                        _ins->InitThreadPool();
                        std::cout << "首次加载对象" << std::endl;
                    }
                }
                pthread_mutex_unlock(&_lock);
    
                return _ins;
            }
    
            void InitThreadPool()
            {
                for (int i = 0; i < _num; i++)
                {
                    pthread_t tid;
                    pthread_create(&tid, nullptr, Routine, (void *)this);
                    sleep(1);
                }
            }
            void PushTask(const T &in)
            {
                Lock();
                _task_queue.push(in);
                Unlock();
                WakeUp();
            }
            void PopTask(T *out)
            {
                *out = _task_queue.front();
                _task_queue.pop();
            }
    
            ~ThreadPool()
            {
                pthread_mutex_destroy(&_mtx);
                pthread_cond_destroy(&_cond);
            }
        };
    
        template <class T>
        ThreadPool<T> *ThreadPool<T>::_ins = nullptr;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
  • 相关阅读:
    typora的主题文件相关(与图片显示有关)
    使用python提供一个简单的restful接口
    Java代码审计之不安全的Java代码
    2023秋招华为技术岗线上面试经历
    HIVE/SQL 实现同一列数据累加和累乘
    保姆级搭建Mysql 并进行视图可视化操作
    【C++】友元函数 ( 友元函数简介 | 友元函数声明 | 友元函数语法 | 友元函数声明不受访问控制限制 | 友元函数参数要求 )
    Python+Selenium WebUI自动化框架 -- 基础操作封装
    [附源码]Python计算机毕业设计Django的高校资源共享平台
    Xilinx FPGA:vivado用串口控制数码管
  • 原文地址:https://blog.csdn.net/JayceSun/article/details/125354640