【Linux】线程池

🌈前言

这篇文章来实现一个线程池!!!


🍨线程池概念

线程池
  • 一种线程使用模式
  • 线程过多会带来调度开销,进而影响缓存局部性和整体性能。而线程池维护着多个线程,等待着监督管理者分配可并发执行的任务
  • 这避免了在处理短时间任务时创建与销毁线程的代价
  • 线程池不仅能够保证内核的充分利用,还能防止过分调度。可用线程数量应该取决于可用的并发处理器、处理器内核、内存、网络sockets等的数量
应用场景
  • 需要大量的线程来完成任务,且完成任务的时间比较短。 WEB服务器完成网页请求这样的任务,使用线程池技术是非常合适的。因为单个任务小,而任务数量巨大,你可以想象一个热门网站的点击次数。 但对于长时间的任务,比如一个Telnet连接请求,线程池的优点就不明显了。因为Telnet会话时间比线程的创建时间大多了
  • 对性能要求苛刻的应用,比如要求服务器迅速响应客户请求
  • 接受突发性的大量请求,但不至于使服务器因此产生大量线程的应用。突发性大量客户请求,在没有线程池情况下,将产生大量线程,虽然理论上大部分操作系统线程数目最大值不是问题,短时间内产生大量线程可能使内存到达极限,出现错误
线程池种类
  • 创建固定数量线程池,循环从任务队列中获取任务对象
  • 获取到任务对象后,执行任务对象中的任务接口

🍨线程池的实现

makefile

test:test.cpp
	g++ -o $@ $^ -std=c++11 -lpthread

.PHONY:clean
clean:
	rm -rf test

Log.hpp – 打印日记,用于debug

#pragma once

// 用于debug -- 打印日志
#include <iostream>
#include <ctime>
#include <pthread.h>

std::ostream &Log()
{
    std::cout << "Fot Debug |" << " timestamp: " << (uint64_t)time(nullptr) << " | " << " Thread[" << pthread_self() << "] | ";
    return std::cout;
}

Task.hpp – 任务类 – 用于派发任务

#pragma once
#include <iostream>

// 设置一个计数器任务
class Task
{
public:
    Task(int Leftoperand = 0, char Operator = '\0', int Rightoperand = 0)
        : Leftoperand_(Leftoperand), Operator_(Operator), Rightoperand_(Rightoperand)
    {
    }
public:
    int run()
    {
        int result = 0;
        switch (Operator_)
        {
        case '+':
            result = Leftoperand_ + Rightoperand_;
            break;
        case '-':
            result = Leftoperand_ - Rightoperand_;
            break;
        case '*':
            result = Leftoperand_ * Rightoperand_;
            break;
        case '/':
            if (Rightoperand_ == 0)
            {
                std::cout << "Divide by zero error! ! !" << std::endl;
                result = -1;
            }
            else
            {
                result = Leftoperand_ / Rightoperand_;
            }
            break;
        case '%':
            if (Rightoperand_ == 0)
            {
                std::cout << "Modulo by zero error! ! !" << std::endl;
                result = -1;
            }
            else
            {
                result = Leftoperand_ % Rightoperand_;
            }
            break;
        default:
            std::cout << "Illegal operator: " << Operator_ << std::endl;
            break;
        }
        return result;
    }
public:
    // 仿函数
    int operator()()
    {
        return run();
    }

    // 输出型函数 -- 获取私有成员
    void GetMemberVal(int* p1, char* p2, int* p3)
    {
        *p1 = Leftoperand_;
        *p2 = Operator_;
        *p3 = Rightoperand_;
    }

private:
    int Leftoperand_;  // 左操作数
    char Operator_;    // 加减乘除字符
    int Rightoperand_; // 右操作数
};

ThreadPool.hpp – 线程池实现 – 注释讲解

#include "Log.hpp"
#include <iostream>
#include <queue>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>

// 线程池 -- 启动一堆线程,等待任务到来,来任务就派发线程去执行任务
// 优先:提高效率,提前启动线程

// 设置方案:启动N个线程 -- 执行线程处理函数 -- 函数里面加锁判断队列是否有任务(条件)
//                     -- 没有任务则在条件变量下阻塞等待,有任务则到pop函数拿任务
//                     -- push函数用于加载任务,并且让条件变量就绪,唤醒线程,处理加载的任务

const int TNum = 5; // 默认设置线程数据为5个

template <typename T>
class ThreadPool
{
public:
    // 构造函数与析构函数
    ThreadPool(int ThreadNum = TNum) : IsStart(false), ThreadNum_(ThreadNum)
    {
        // 线程数量不能小于或等于0
        if (ThreadNum_ < 0)
        {
            std::cout << "The number of threads cannot be less than 1 " << std::endl;
            exit(1);
        }
        pthread_mutex_init(&Pmutex_, nullptr);
        pthread_cond_init(&Pcond_, nullptr);
    }

    ~ThreadPool()
    {
        pthread_mutex_destroy(&Pmutex_);
        pthread_cond_destroy(&Pcond_);
    }

public:
    // 线程处理任务函数 -- 设置全局处理函数 -- 类成员函数包含隐藏this指针
    static void *TaskProcess(void *args)
    {
        // 线程分离 -- 线程执行完任务后,自动释放资源,主线程不用等待
        pthread_detach(pthread_self());
        ThreadPool<T> *tpp = static_cast<ThreadPool<T> *>(args);

        while (true)
        {
            tpp->lockQueue();          // 加锁
            while (!(tpp->IshaveTask())) // 判断是否有任务,有返回1,没有返回0
            {
                // 没有任务就在条件变量下阻塞等待,等待push加载任务后唤醒
                tpp->waitForTask();
            }
            // 这个任务就被拿到了线程的上下文中
            T t = tpp->pop();

            tpp->unlockQueue(); // 解锁

            // ....下面是并发的处理任务 -- 约定好任务必须有run成员函数
            int result = t.run();
            int Leftoperand;
            char Operator;
            int Rightoperand;
            t.GetMemberVal(&Leftoperand, &Operator, &Rightoperand);
            // 打印日志 -- debug
            Log() << Leftoperand << Operator << Rightoperand << " = " << result << std::endl;
        }

        return nullptr;
    }

    // 启动多线程
    void StartUpThread()
    {
        if (IsStart != false)
        {
            std::cout << "Error: Start thread repeatedly! ! !" << std::endl;
            exit(2);
        }
        for (int i = 0; i < ThreadNum_; ++i)
        {
            pthread_t tid;
            // 这里传this指针,是因为处理函数是在类里面(类成员函数包含隐藏this指针)-- error: 类型不符合
            pthread_create(&tid, nullptr, TaskProcess, this);
        }
        // 启动完成,更新IsStart为true
        IsStart = true;
    }

    // 加载任务
    void push(const T &val)
    {
        // 加锁 -- 加载任务 -- 派发任务给线程 --解锁
        lockQueue();
        threadpool_.push(val);
        WakeUp();
        unlockQueue();
    }

private:
    void lockQueue() { pthread_mutex_lock(&Pmutex_); }
    void unlockQueue() { pthread_mutex_unlock(&Pmutex_); }
    void WakeUp() { pthread_cond_signal(&Pcond_); }
    void waitForTask() { pthread_cond_wait(&Pcond_, &Pmutex_); }
    bool IshaveTask() { return !(threadpool_.empty()); }
    // 线程处理任务后,需要在队列中删除任务 -- 并且返回这个任务(处理函数要对任务进行处理)
    T pop()
    {
        T val = threadpool_.front();
        threadpool_.pop();
        return val;
    }

private:
    bool IsStart;              // 防止重复启动线程
    int ThreadNum_;            // 线程数量
    std::queue<T> threadpool_; // 线程池
    pthread_mutex_t Pmutex_;   // 互斥锁
    pthread_cond_t Pcond_;     // 条件变量
};

test.cpp

#include "ThreadPool.hpp"
#include "Task.hpp"
#include <memory>
#include <string>
#include <ctime>
using namespace std;

int main()
{
    unique_ptr<ThreadPool<Task>> tp(new ThreadPool<Task>());
    string OperatorSet = "+-*/%";
    tp->StartUpThread();

    // 设置随机数种子 -- 按位异或是为了更随机一些
    srand((unsigned long)time(nullptr) ^ getpid() ^ pthread_self());
    while (true)
    {
        int Leftoperand = rand() % 50; // 取值范围:[0, 40)
        char Operator = OperatorSet[rand() % OperatorSet.size()];
        int Rightoperand = rand() % 10; // 取值范围:[0, 10)
        Task t(Leftoperand, Operator, Rightoperand);
        Log() << Leftoperand << Operator << Rightoperand << " = " << "???" << std::endl;
        tp->push(t);
        usleep(10000);
    }
    return 0;
}

在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值