线程池单例化

#include <iostream>
#include <pthread.h>
#include <vector>
#include <queue>
#include <unistd.h>

#include "thread.hpp"
#include "lockGuard.hpp"
#include "log.hpp"

const int default_capacity = 5;

template <class T>
class ThreadPool
{
private:
    // 构造函数
    ThreadPool(int capacity = default_capacity)
        : _capacity(capacity)
    {
        pthread_mutex_init(&_mtx, nullptr);
        pthread_cond_init(&_cond, nullptr);
        for (int i = 0; i < capacity; i++)
        {
            Thread *ptd = new Thread(i + 1, routine, this);
            _threads.push_back(ptd);
        }
    }
    // 拷贝构造函数
    ThreadPool(const ThreadPool<T> &other) = delete;
    // 赋值运算符
    const ThreadPool<T> &operator=(const ThreadPool<T> &other) = delete;

public:
    static ThreadPool<T> *getInstance(int capacity = default_capacity)
    {
        if (thread_ptr == nullptr)
        {

            lockGuard lockguard(&mutex);
            if (thread_ptr == nullptr)
            {
                thread_ptr = new ThreadPool<T>(capacity);
            }
        }
        return thread_ptr;
    }

    bool isEmpty()
    {
        return _task_queue.empty();
    }

    static void *routine(void *arg)
    {
        ThreadData *td = (ThreadData *)arg;
        ThreadPool<T> *tp = (ThreadPool<T> *)td->_args;
        while (true)
        {
            lockGuard lockguard(&tp->_mtx);
            if (tp->isEmpty())
            {
                pthread_cond_wait(&tp->_cond, &tp->_mtx);
            }
            T task = tp->_task_queue.front();
            tp->_task_queue.pop();
            task(td->_name);
            sleep(1);
        }
    }

    void run()
    {
        for (auto &iter : _threads)
        {
            iter->start();
            // std::cout << iter->name() << " 启动成功" << std::endl;
            logMessage(NORMAL, "%s%s", iter->name().c_str(), "启动成功");
        }
    }

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

    void joins()
    {
        for (int i = 0; i < _capacity; i++)
        {
            _threads[i]->join();
        }
    }

    ~ThreadPool()
    {
        for (int i = 0; i < _capacity; i++)
        {
            _threads[i]->join();
            delete _threads[i];
        }
        pthread_mutex_destroy(&_mtx);
        pthread_cond_destroy(&_cond);
    }

private:
    std::vector<Thread *> _threads; // 存放线程
    size_t _capacity;

    std::queue<T> _task_queue; // 存放任务

    pthread_mutex_t _mtx;
    pthread_cond_t _cond;

    static ThreadPool<T> *thread_ptr; // 声明
    static pthread_mutex_t mutex;
};

template <class T>
static ThreadPool<T> *ThreadPool<T>::thread_ptr = nullptr;

template <class T>
static pthread_mutex_t ThreadPool<T>::mutex = PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值