基于C++11实现线程池

为何需要线程池

那么为什么我们需要线程池技术呢?多线程编程用的好好的,干嘛还要引入线程池这个东西呢?引入一个新的技术肯定不是为了装酷,肯定是为了解决某个问题的,而服务端一般都是效率问题。

我们可以看到多线程提高了CPU的使用率和程序的工作效率,但是如果有大量的线程,就会影响性能,因为要大量的创建与销毁,因为CPU需要在它们之间切换。线程池可以想象成一个池子,它的作用就是让每一个线程结束后,并不会销毁,而是放回到线程池中成为空闲状态,等待下一个对象来使用。

C++中的线程池

但是让人遗憾的是,C++并没有在语言级别上支持线程池技术,总感觉C++委员会对多线程的支持像是犹抱琵琶半遮面的羞羞女一样,无法完全的放开。

虽然无法从语言级别上支持,但是我们可以利用条件变量和互斥锁自己实现一个线程池。这里就不得不啰嗦几句,条件变量和互斥锁就像两把利剑,几乎可以实现多线程技术中的大部分问题,不管是生产消费者模型,还是线程池,亦或是信号量,所以我们必须好好掌握好这两个工具。

任务队列(Task Queue)

线程池中的线程会持续查询任务队列是否有可用工作。当两个甚至多个线程试图同时执行查询工作时,这会引起难以估计的灾难。因而我们需要对C++的std::queue进行包装,实现一个线程安全的SafeQueue。

template <typename T>
class SafeQueue
{
private:
    std::queue<T> m_queue; //利用模板函数构造队列
​
    std::mutex m_mutex; // 访问互斥信号量public:
    SafeQueue() {}
    SafeQueue(SafeQueue &&other) {}
    ~SafeQueue() {}bool empty() // 返回队列是否为空
    {
        std::unique_lock<std::mutex> lock(m_mutex); // 互斥信号变量加锁,防止m_queue被改变return m_queue.empty();
    }int size()
    {
        std::unique_lock<std::mutex> lock(m_mutex); // 互斥信号变量加锁,防止m_queue被改变return m_queue.size();
    }// 队列添加元素
    void enqueue(T &t)
    {
        std::unique_lock<std::mutex> lock(m_mutex);
        m_queue.emplace(t);
    }// 队列取出元素
    bool dequeue(T &t)
    {
        std::unique_lock<std::mutex> lock(m_mutex); // 队列加锁if (m_queue.empty())
            return false;
        t = std::move(m_queue.front()); // 取出队首元素,返回队首元素值,并进行右值引用
​
        m_queue.pop(); // 弹出入队的第一个元素return true;
    }
};

提交函数

线程池最重要的方法就是负责向任务队列添加任务。我们的提交函数应该做到以下两点:

  1. 接收任何参数的任何函数。(普通函数,Lambda,成员函数……)
  2. 立即返回“东西”,避免阻塞主线程。这里返回的“东西”或者说“对象”应该包含任务结束的结果。
// Submit a function to be executed asynchronously by the pool
    template <typename F, typename... Args>
    auto submit(F &&f, Args &&...args) -> std::future<decltype(f(args...))> 
    {
        // Create a function with bounded parameter ready to execute
        std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...); // 连接函数和参数定义,特殊函数类型,避免左右值错误// Encapsulate it into a shared pointer in order to be able to copy construct
        auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);// Warp packaged task into void function
        std::function<void()> warpper_func = [task_ptr]()
        {
            (*task_ptr)();
        };// 队列通用安全封包函数,并压入安全队列
        m_queue.enqueue(warpper_func);// 唤醒一个等待中的线程
        m_conditional_lock.notify_one();// 返回先前注册的任务指针
        return task_ptr->get_future();
    }

C++11众多的语法糖正式来袭。下面讲一下需要注意的地方:

1.submit()是一个模板函数,这很明显。template<typename F, typename… Args>中的typename… Args是C++11引入的可变模版参数(variadic templates),很容易理解。
首先来看长得奇奇怪怪的函数头部分,auto submit(F &&f, Args &&…args) -> std::future<decltype(f(args…))>,这里函数类型的定义用到了叫做“尾返回类型推导”的技巧。
按照标准,auto关键字不能用于函数形参的类型推导,在C++14以前,也不能直接用auto func()的形式来推导函数的返回类型。
因此传统C++中我们必须这么写:

template<typename R, typename T, typename U>
R add(T x, U y) {
 return x+y;
}

这样存在很明显的缺陷:事实上很多时候我们并不知道add()这个函数会进行什么操作,获取什么样的返回类型。
最终在C++11中这个问题得到了解决。C++11关键字decltype解决了auto关键字只能对变量类型进行类型推导的缺陷。它的用法也很简单,应该也是看过C++11标准就能记住的:
decltype(表达式)
但是为了利用decltype来推导函数的返回类型,我们并不能直接写出这种形式的代码:
decltype(x+y) add(T x, U y)
因为编译器在读到decltype(x+y)时,x和y尚未定义。而这个问题的解决方案,正是尾返回类型推导。C++11引入了一个尾返回类型(trailing return type),利用auto关键字将返回类型后置:

template<typename T, typename U>
auto add2(T x, U y) -> decltype(x+y){
return x + y;
}

至此,看起来奇奇怪怪的函数头中关于函数的返回类型的定义已经清楚明了:该函数的返回值将从std::future<decltype(f(args…))>中自动推导得出。
接着谈函数头。这里我们谈一下std::future,它提供了一个访问异步操作结果的途径。我们可以使用std::future的wait()方法来设置屏障,阻塞线程,实现线程同步。并最终使用std::future的get()方法来获得执行结果。

完整代码

//thread_pool.h#ifndef THREAD_POOL_H
#define THREAD_POOL_H#include <mutex>
#include <queue>
#include <functional>
#include <future>
#include <thread>
#include <utility>
#include <vector>// Thread safe implementation of a Queue using a std::queue
template <typename T>
class SafeQueue
{
private:
    std::queue<T> m_queue; //利用模板函数构造队列
​
    std::mutex m_mutex; // 访问互斥信号量public:
    SafeQueue() {}
    SafeQueue(SafeQueue &&other) {}
    ~SafeQueue() {}bool empty() // 返回队列是否为空
    {
        std::unique_lock<std::mutex> lock(m_mutex); // 互斥信号变量加锁,防止m_queue被改变return m_queue.empty();
    }int size()
    {
        std::unique_lock<std::mutex> lock(m_mutex); // 互斥信号变量加锁,防止m_queue被改变return m_queue.size();
    }// 队列添加元素
    void enqueue(T &t)
    {
        std::unique_lock<std::mutex> lock(m_mutex);
        m_queue.emplace(t);
    }// 队列取出元素
    bool dequeue(T &t)
    {
        std::unique_lock<std::mutex> lock(m_mutex); // 队列加锁if (m_queue.empty())
            return false;
        t = std::move(m_queue.front()); // 取出队首元素,返回队首元素值,并进行右值引用
​
        m_queue.pop(); // 弹出入队的第一个元素return true;
    }
};class ThreadPool
{
private:
    class ThreadWorker // 内置线程工作类
    {
    private:
        int m_id; // 工作id
​
        ThreadPool *m_pool; // 所属线程池
    public:
        // 构造函数
        ThreadWorker(ThreadPool *pool, const int id) : m_pool(pool), m_id(id)
        {
        }// 重载()操作
        void operator()()
        {
            std::function<void()> func; // 定义基础函数类funcbool dequeued; // 是否正在取出队列中元素while (!m_pool->m_shutdown)
            {
                {
                    // 为线程环境加锁,互访问工作线程的休眠和唤醒
                    std::unique_lock<std::mutex> lock(m_pool->m_conditional_mutex);// 如果任务队列为空,阻塞当前线程
                    if (m_pool->m_queue.empty())
                    {
                        m_pool->m_conditional_lock.wait(lock); // 等待条件变量通知,开启线程
                    }// 取出任务队列中的元素
                    dequeued = m_pool->m_queue.dequeue(func);
                }// 如果成功取出,执行工作函数
                if (dequeued)
                    func();
            }
        }
    };bool m_shutdown; // 线程池是否关闭
​
    SafeQueue<std::function<void()>> m_queue; // 执行函数安全队列,即任务队列
​
    std::vector<std::thread> m_threads; // 工作线程队列
​
    std::mutex m_conditional_mutex; // 线程休眠锁互斥变量
​
    std::condition_variable m_conditional_lock; // 线程环境锁,可以让线程处于休眠或者唤醒状态public:
    // 线程池构造函数
    ThreadPool(const int n_threads = 4)
        : m_threads(std::vector<std::thread>(n_threads)), m_shutdown(false)
    {
    }ThreadPool(const ThreadPool &) = delete;ThreadPool(ThreadPool &&) = delete;
​
    ThreadPool &operator=(const ThreadPool &) = delete;
​
    ThreadPool &operator=(ThreadPool &&) = delete;// Inits thread pool
    void init()
    {
        for (int i = 0; i < m_threads.size(); ++i)
        {
            m_threads.at(i) = std::thread(ThreadWorker(this, i)); // 分配工作线程
        }
    }// Waits until threads finish their current task and shutdowns the pool
    void shutdown()
    {
        m_shutdown = true;
        m_conditional_lock.notify_all(); // 通知,唤醒所有工作线程for (int i = 0; i < m_threads.size(); ++i)
        {
            if (m_threads.at(i).joinable()) // 判断线程是否在等待
            {
                m_threads.at(i).join(); // 将线程加入到等待队列
            }
        }
    }// Submit a function to be executed asynchronously by the pool
    template <typename F, typename... Args>
    auto submit(F &&f, Args &&...args) -> std::future<decltype(f(args...))>
    {
        // Create a function with bounded parameter ready to execute
        std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...); // 连接函数和参数定义,特殊函数类型,避免左右值错误// Encapsulate it into a shared pointer in order to be able to copy construct
        auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);// Warp packaged task into void function
        std::function<void()> warpper_func = [task_ptr]()
        {
            (*task_ptr)();
        };// 队列通用安全封包函数,并压入安全队列
        m_queue.enqueue(warpper_func);// 唤醒一个等待中的线程
        m_conditional_lock.notify_one();// 返回先前注册的任务指针
        return task_ptr->get_future();
    }
};#endif

测试代码

// test.cpp#include <iostream>
#include <random>
#include "thread_pool.h"
std::random_device rd; // 真实随机数产生器
​
std::mt19937 mt(rd()); //生成计算随机数mt
​
std::uniform_int_distribution<int> dist(-1000, 1000); //生成-1000到1000之间的离散均匀分布数auto rnd = std::bind(dist, mt);// 设置线程睡眠时间
void simulate_hard_computation()
{
    std::this_thread::sleep_for(std::chrono::milliseconds(2000 + rnd()));
}// 添加两个数字的简单函数并打印结果
void multiply(const int a, const int b)
{
    simulate_hard_computation();
    const int res = a * b;
    std::cout << a << " * " << b << " = " << res << std::endl;
}// 添加并输出结果
void multiply_output(int &out, const int a, const int b)
{
    simulate_hard_computation();
    out = a * b;
    std::cout << a << " * " << b << " = " << out << std::endl;
}// 结果返回
int multiply_return(const int a, const int b)
{
    simulate_hard_computation();
    const int res = a * b;
    std::cout << a << " * " << b << " = " << res << std::endl;
    return res;
}void example()
{
    // 创建3个线程的线程池
    ThreadPool pool(3);// 初始化线程池
    pool.init();// 提交乘法操作,总共30个
    for (int i = 1; i <= 3; ++i)
        for (int j = 1; j <= 10; ++j)
        {
            pool.submit(multiply, i, j);
        }// 使用ref传递的输出参数提交函数
    int output_ref;
    auto future1 = pool.submit(multiply_output, std::ref(output_ref), 5, 6);// 等待乘法输出完成
    future1.get();
    std::cout << "Last operation result is equals to " << output_ref << std::endl;// 使用return参数提交函数
    auto future2 = pool.submit(multiply_return, 5, 3);// 等待乘法输出完成
    int res = future2.get();
    std::cout << "Last operation result is equals to " << res << std::endl;// 关闭线程池
    pool.shutdown();
}int main()
{
    example();return 0;
}
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值