C++实现线程池

线程池:

  线程池采用预创建的技术,在应用程序启动之后,将立即创建一定数量的线程(N1),放入空闲队列中。这些线程都是处于阻塞状态,不消耗CPU,但占用较小的内存空间。当任务到来后,缓冲池选择一个空闲线程,把任务传入此线程中运行。

线程池使用时机:

  因为线程池可以减少线程本身创建和销毁带来的开销。所以如果线程本身的开销相对于线程任务执行开销而言是可以忽略不计的,那么就没有必要采用线程池;下面两种情况才比较适合使用线程池:

    (1) 单位时间内处理任务频繁而且任务处理时间短;
    (2) 对实时性要求较高。如果接受到任务后在创建线程,可能满足不了实时要求,因此必须采用线程池进行预创建。

 

代码实现(参考:https://github.com/progschj/ThreadPool

    线程池的实现思想:“管理一个任务队列,一个线程队列,然后每次取一个任务分配给一个线程去做,循环往复。”

头文件ThreadPool.h的内容如下

#ifndef THREAD_POOL_H
#define THREAD_POOL_H
 
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
 
class ThreadPool {
 
public:
    ThreadPool(size_t);                          //构造函数
    template<class F, class... Args>             //类模板
    int enqueue(F&& f, Args&&... args);//任务队列
    ~ThreadPool();                              //析构函数
  
bool cancel(int sn);               //取消任务
private:
    std::vector< std::thread > workers;            //线程队列
    std::queue< std::function<void()> > tasks;     //任务队列   
 
    std::mutex queue_mutex;                        //互斥量
    std::condition_variable condition;             //条件变量
    bool stop;                                     //停止
   int sn;                        //取消索引
};
 
// 构造函数,把线程插入线程队列,插入时调用embrace_back(),用匿名函数lambda初始化Thread对象
inline ThreadPool::ThreadPool(size_t threads) : stop(false),sn(0){
 
    for(size_t i = 0; i<threads; ++i)
  //c11建议使用emplace_back,它的好处是,不需要两次构造,直接传入构造函数的参数,只需要在真正进入vector的时候进行一次构造,所以这里传入的是一个lamda表达式形式的函数,这个函数是std::Thread类构造的参数,在这里直接通过emplace_back构造并同时存入vector中。 workers.emplace_back( [
this] { for(;;) { // task从任务队列接收任务(std::function传入的是函数类型  返回值 (参数类型) 如:std::function<void (int)>) std::function<void()> task; { //加锁,锁对象生命周期结束后自动解锁 std::unique_lock<std::mutex> lock(this->queue_mutex); //(1)当匿名函数返回false时才阻塞线程,阻塞时自动释放锁。 //(2)当匿名函数返回true且受到通知时解阻塞,然后加锁。 this->condition.wait(lock,[this]{ return this->stop || !this->tasks.empty(); }); if(this->stop && this->tasks.empty()) return; //从任务队列取出一个任务 task = std::move(this->tasks.front()); this->tasks.pop(); } // 自动解锁 task(); // 执行这个任务 } } ); } // 添加新的任务到任务队列 template<class F, class... Args> int ThreadPool::enqueue(F&& f, Args&&... args) { // 获取函数返回值类型 typename:指定别名,和typedef的作用类似; using return_type = typename std::result_of<F(Args...)>::type; // 创建一个指向任务的指针 auto task = std::make_shared< std::packaged_task<return_type()> >( std::bind(std::forward<F>(f), std::forward<Args>(args)...) ); std::future<return_type> res = task->get_future(); { std::unique_lock<std::mutex> lock(queue_mutex); //加锁 if(stop) throw std::runtime_error("enqueue on stopped ThreadPool");
       sn++;
        tasks.emplace([task](){ (*task)(); });          //把任务加入队列
    }                                                   //自动解锁
    condition.notify_one();                             //通知条件变量,唤醒一个线程
    return sn;
}
//取消一个任务
inline bool ThreadPool::cancel(int sn)
{
    std::unique_lock<std::mutex> lock(queue_mutex);
    auto iter = tasks.begin();
    while (iter != tasks.end())
    {
        if(iter->first == sn)
        {
            tasks.erase(iter);
            return true;
        }
        ++iter;
    }
    return false;
}
// 析构函数,阻塞主线程直到删除所有线程
inline ThreadPool::~ThreadPool()
{
    {
        std::unique_lock<std::mutex> lock(queue_mutex);
        stop = true;
    }
    condition.notify_all();
    for(std::thread &worker: workers)
        worker.join();
}
 
#endif

example:

#include <iostream>
#include "ThreadPool.h"
 
void func()
{
  return;
}
 
int main()
{
    ThreadPool pool(1);
    pool.enqueue(fun);
}

  

posted @ 2020-09-08 22:40  吉尔加斯  阅读(1381)  评论(0编辑  收藏  举报