<Linux> 实现简易线程池

文章目录

ThreadPool.hpp

c 复制代码
#pragma once
#include <vector>
#include <queue>
#include <iostream>

#include "Thread.hpp"
#include "LockGuard.hpp"

#define g_threadPool_num 5
template <class T>
class ThreadPool
{
public:
    bool isEmpty()
    {
        return _task_queue.empty();
    }

    void waitCond()
    {
        pthread_cond_wait(&_cond, &_mtx);
    }

    T getTask()
    {
        T t = _task_queue.front();
        _task_queue.pop();

        return t;
    }

public:
    ThreadPool(int threadNum = g_threadPool_num)
        : _threads(threadNum)
    {
        pthread_cond_init(&_cond, nullptr);
        pthread_mutex_init(&_mtx, nullptr);
        for (size_t i = 0; i < threadNum; i++)
        {
            _threads[i] = new Thread(routine, this, i + 1);
        }
    }

    static void *routine(void *args)
    {
        ThreadData *td = (ThreadData *)args;
        ThreadPool<T> *tp = (ThreadPool<T> *)td->_args;
        while (true)
        {
            T task(-1);
            {
                LockGuard lock(&tp->_mtx);
                while (tp->isEmpty())
                {
                    tp->waitCond();
                }
                task = tp->getTask();
            }
            // 仿函数
            task(td->_name);
        }

        return nullptr;
    }

    void pushTask(const T &task)
    {
        LockGuard lock(&_mtx);
        _task_queue.push(task);
        pthread_cond_signal(&_cond);
    }

    void run()
    {
        for (auto &e : _threads)
        {
            e->start();
        }
    }

    ~ThreadPool()
    {

        for (auto &e : _threads)
        {
            e->join();
        }
        pthread_cond_destroy(&_cond);
        pthread_mutex_destroy(&_mtx);
    }

private:
    std::vector<Thread *> _threads;
    std::queue<T> _task_queue;
    pthread_mutex_t _mtx;
    pthread_cond_t _cond;
};

Thread.hpp

c 复制代码
#pragma once
#include <string>
#include <pthread.h>
#include <iostream>
typedef void *(*fun_t)(void *);

class ThreadData
{
public:
    void *_args;
    std::string _name;
    pthread_t _tid;
    fun_t _fun;
};
class Thread
{
public:
    Thread(fun_t callback, void *args, int num)
    {
        char nameBuffer[64] = "Thread --- ";
        sprintf(nameBuffer, "%s%d", nameBuffer, num);
        _td._name = nameBuffer;
        _td._args = args;
        _td._fun = callback;
    }

    void start()
    {
        pthread_create(&_td._tid, nullptr, _td._fun, &_td);
    }

    void join()
    {
        pthread_join(_td._tid, nullptr);
    }

public:
    ThreadData _td;
};

LockGuard.hpp

c 复制代码
#pragma once

#include <pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t *mtx)
        : _pmtx(mtx)
    {
        pthread_mutex_init(_pmtx, nullptr);
    }

    void lock()
    {
        pthread_mutex_lock(_pmtx);
    }

    void unlock()
    {
        pthread_mutex_unlock(_pmtx);
    }

    ~Mutex()
    {

        pthread_mutex_destroy(_pmtx);
    }

private:
    pthread_mutex_t *_pmtx;
};

class LockGuard
{
public:
    LockGuard(pthread_mutex_t *mtx)
        : _mtx(mtx)
    {
        _mtx.lock();
    }

    ~LockGuard()
    {
        _mtx.unlock();
    }

private:
    Mutex _mtx;
};

Task.hpp

c 复制代码
#pragma once
#include <iostream>
class Task
{

public:
    Task(int num)
        : _num(num)
    {
    }

    void operator()(const std::string &name)
    {
        std::cout << name << " 执行" << _num << "任务" << std::endl;
    }

private:
    int _num;
};

test.cc

c 复制代码
#include "ThreadPool.hpp"
#include "Task.hpp"
#include <unistd.h>
int main()
{
    ThreadPool<Task> tp;
    tp.run();

    int index = 0;
    while (true)
    {
        Task t(index++);
        tp.pushTask(t);
        sleep(1);
    }

    return 0;
}
相关推荐
深鱼~6 分钟前
Linux系统部署MongoDB开源文档型数据库并实现无公网IP远程访问
linux·数据库·mongodb
玄武 剑13 分钟前
开源自动化热键映射工具autohotkey十大用法及精选脚本
运维·自动化
登云时刻13 分钟前
【问题记录】Jenkins Pipeline读取变量的各种方法
运维·jenkins
weishenjieneng17 分钟前
微深节能 煤码头自动化翻堆及取料集控系统 格雷母线
运维·自动化
威斯盾科技23 分钟前
电力设备巡检管理系统
运维·网络·信息可视化
zhishengwangxiao27 分钟前
职升网:一级计量师证书含金量有多少?
运维·服务器
cpp_learners34 分钟前
Linux 程序卡死的特殊处理
linux·shell·c/c++·程序卡死·守护程序
科学的发展-只不过是读大自然写的代码37 分钟前
ubuntu 进入命令行
linux·运维·ubuntu
念晚91741 分钟前
Linux——测网速例子,在网站查看生成的图片
linux·运维·服务器·网络·数据库
运维Linux和python1 小时前
从nginx返回404来看http1.0和http1.1的区别
运维·nginx