基于C++11的线程池(threadpool),简洁且可以带任意多的参数

咳咳。C++11 加入了线程库,从此告别了标准库不支持并发的历史。然而 c++ 对于多线程的支持还是比较低级,稍微高级一点的用法都需要自己去实现,譬如线程池、信号量等。线程池(thread pool)这个东西,在面试上多次被问到,一般的回答都是:“管理一个任务队列,一个线程队列,然后每次取一个任务分配给一个线程去做,循环往复。” 貌似没有问题吧。但是写起程序来的时候就出问题了。

废话不多说,先上实现,然后再啰嗦。(dont talk, show me ur code !)

代码实现

 文末有GitHub 链接, 但是没人去看更新的, 现在更新下好了--- 2022/06/02

  1 #pragma once
  2 #ifndef THREAD_POOL_H
  3 #define THREAD_POOL_H
  4 
  5 #include <vector>
  6 #include <queue>
  7 #include <atomic>
  8 #include <future>
  9 //#include <condition_variable>
 10 //#include <thread>
 11 //#include <functional>
 12 #include <stdexcept>
 13 
 14 namespace std
 15 {
 16 //线程池最大容量,应尽量设小一点
 17 #define  THREADPOOL_MAX_NUM 16
 18 //线程池是否可以自动增长(如果需要,且不超过 THREADPOOL_MAX_NUM)
 19 //#define  THREADPOOL_AUTO_GROW
 20 
 21 //线程池,可以提交变参函数或拉姆达表达式的匿名函数执行,可以获取执行返回值
 22 //不直接支持类成员函数, 支持类静态成员函数或全局函数,Opteron()函数等
 23 class threadpool
 24 {
 25     unsigned short _initSize;       //初始化线程数量
 26     using Task = function<void()>; //定义类型
 27     vector<thread> _pool;          //线程池
 28     queue<Task> _tasks;            //任务队列
 29     mutex _lock;                   //任务队列同步锁
 30 #ifdef THREADPOOL_AUTO_GROW
 31     mutex _lockGrow;               //线程池增长同步锁
 32 #endif // !THREADPOOL_AUTO_GROW
 33     condition_variable _task_cv;   //条件阻塞
 34     atomic<bool> _run{ true };     //线程池是否执行
 35     atomic<int>  _idlThrNum{ 0 };  //空闲线程数量
 36 
 37 public:
 38     inline threadpool(unsigned short size = 4) { _initSize = size; addThread(size); }
 39     inline ~threadpool()
 40     {
 41         _run=false;
 42         _task_cv.notify_all(); // 唤醒所有线程执行
 43         for (thread& thread : _pool) {
 44             //thread.detach(); // 让线程“自生自灭”
 45             if (thread.joinable())
 46                 thread.join(); // 等待任务结束, 前提:线程一定会执行完
 47         }
 48     }
 49 
 50 public:
 51     // 提交一个任务
 52     // 调用.get()获取返回值会等待任务执行完,获取返回值
 53     // 有两种方法可以实现调用类成员,
 54     // 一种是使用   bind: .commit(std::bind(&Dog::sayHello, &dog));
 55     // 一种是用   mem_fn: .commit(std::mem_fn(&Dog::sayHello), this)
 56     template<class F, class... Args>
 57     auto commit(F&& f, Args&&... args) -> future<decltype(f(args...))>
 58     {
 59         if (!_run)    // stoped ??
 60             throw runtime_error("commit on ThreadPool is stopped.");
 61 
 62         using RetType = decltype(f(args...)); // typename std::result_of<F(Args...)>::type, 函数 f 的返回值类型
 63         auto task = make_shared<packaged_task<RetType()>>(
 64             bind(forward<F>(f), forward<Args>(args)...)
 65         ); // 把函数入口及参数,打包(绑定)
 66         future<RetType> future = task->get_future();
 67         {    // 添加任务到队列
 68             lock_guard<mutex> lock{ _lock };//对当前块的语句加锁  lock_guard 是 mutex 的 stack 封装类,构造的时候 lock(),析构的时候 unlock()
 69             _tasks.emplace([task]() { // push(Task{...}) 放到队列后面
 70                 (*task)();
 71             });
 72         }
 73 #ifdef THREADPOOL_AUTO_GROW
 74         if (_idlThrNum < 1 && _pool.size() < THREADPOOL_MAX_NUM)
 75             addThread(1);
 76 #endif // !THREADPOOL_AUTO_GROW
 77         _task_cv.notify_one(); // 唤醒一个线程执行
 78 
 79         return future;
 80     }
 81 
 82     //空闲线程数量
 83     int idlCount() { return _idlThrNum; }
 84     //线程数量
 85     int thrCount() { return _pool.size(); }
 86 
 87 #ifndef THREADPOOL_AUTO_GROW
 88 private:
 89 #endif // !THREADPOOL_AUTO_GROW
 90     //添加指定数量的线程
 91     void addThread(unsigned short size)
 92     {
 93 #ifndef THREADPOOL_AUTO_GROW
 94         if (!_run)    // stoped ??
 95             throw runtime_error("Grow on ThreadPool is stopped.");
 96         unique_lock<mutex> lockGrow{ _lockGrow }; //自动增长锁
 97 #endif // !THREADPOOL_AUTO_GROW
 98         for (; _pool.size() < THREADPOOL_MAX_NUM && size > 0; --size)
 99         {   //增加线程数量,但不超过 预定义数量 THREADPOOL_MAX_NUM
100             _pool.emplace_back( [this]{ //工作线程函数
101                 while (true) //防止 _run==false 时立即结束,此时任务队列可能不为空
102                 {
103                     Task task; // 获取一个待执行的 task
104                     {
105                         // unique_lock 相比 lock_guard 的好处是:可以随时 unlock() 和 lock()
106                         unique_lock<mutex> lock{ _lock };
107                         _task_cv.wait(lock, [this] { // wait 直到有 task, 或需要停止
108                             return !_run || !_tasks.empty();
109                         });
110                         if (!_run && _tasks.empty())
111                             return;
112                         _idlThrNum--;
113                         task = move(_tasks.front()); // 按先进先出从队列取一个 task
114                         _tasks.pop();
115                     }
116                     task();//执行任务
117 #ifndef THREADPOOL_AUTO_GROW
118                     if (_idlThrNum>0 && _pool.size() > _initSize) //支持自动释放空闲线程,避免峰值过后大量空闲线程
119                         return;
120 #endif // !THREADPOOL_AUTO_GROW
121                     {
122                         unique_lock<mutex> lock{ _lock };
123                         _idlThrNum++;
124                     }
125                 }
126             });
127             {
128                 unique_lock<mutex> lock{ _lock };
129                 _idlThrNum++;
130             }
131         }
132     }
133 };
134 
135 }
136 
137 #endif  //https://github.com/lzpong/

 

代码不多吧,上百行代码就完成了 线程池, 并且, 看看 commit,  哈,  不是固定参数的, 无参数数量限制!  这得益于可变参数模板.

怎么使用?

 看下面代码(展开查看)

 1 #include "threadpool.h"
 2 #include <iostream>
 3 
 4 void fun1(int slp)
 5 {
 6     printf("  hello, fun1 !  %d\n" ,std::this_thread::get_id());
 7     if (slp>0) {
 8         printf(" ======= fun1 sleep %d  =========  %d\n",slp, std::this_thread::get_id());
 9         std::this_thread::sleep_for(std::chrono::milliseconds(slp));
10     }
11 }
12 
13 struct gfun {
14     int operator()(int n) {
15         printf("%d  hello, gfun !  %d\n" ,n, std::this_thread::get_id() );
16         return 42;
17     }
18 };
19 
20 class A { 
21 public:
22     static int Afun(int n = 0) {   //函数必须是 static 的才能直接使用线程池
23         std::cout << n << "  hello, Afun !  " << std::this_thread::get_id() << std::endl;
24         return n;
25     }
26 
27     static std::string Bfun(int n, std::string str, char c) {
28         std::cout << n << "  hello, Bfun !  "<< str.c_str() <<"  " << (int)c <<"  " << std::this_thread::get_id() << std::endl;
29         return str;
30     }
31 };
32 
33 int main()
34     try {
35         std::threadpool executor{ 50 };
36         A a;
37         std::future<void> ff = executor.commit(fun1,0);
38         std::future<int> fg = executor.commit(gfun{},0);
39         std::future<int> gg = executor.commit(a.Afun, 9999); //IDE提示错误,但可以编译运行
40         std::future<std::string> gh = executor.commit(A::Bfun, 9998,"mult args", 123);
41         std::future<std::string> fh = executor.commit([]()->std::string { std::cout << "hello, fh !  " << std::this_thread::get_id() << std::endl; return "hello,fh ret !"; });
42 
43         std::cout << " =======  sleep ========= " << std::this_thread::get_id() << std::endl;
44         std::this_thread::sleep_for(std::chrono::microseconds(900));
45 
46         for (int i = 0; i < 50; i++) {
47             executor.commit(fun1,i*100 );
48         }
49         std::cout << " =======  commit all ========= " << std::this_thread::get_id()<< " idlsize="<<executor.idlCount() << std::endl;
50 
51         std::cout << " =======  sleep ========= " << std::this_thread::get_id() << std::endl;
52         std::this_thread::sleep_for(std::chrono::seconds(3));
53 
54         ff.get(); //调用.get()获取返回值会等待线程执行完,获取返回值
55         std::cout << fg.get() << "  " << fh.get().c_str()<< "  " << std::this_thread::get_id() << std::endl;
56 
57         std::cout << " =======  sleep ========= " << std::this_thread::get_id() << std::endl;
58         std::this_thread::sleep_for(std::chrono::seconds(3));
59 
60         std::cout << " =======  fun1,55 ========= " << std::this_thread::get_id() << std::endl;
61         executor.commit(fun1,55).get();    //调用.get()获取返回值会等待线程执行完
62 
63         std::cout << "end... " << std::this_thread::get_id() << std::endl;
64 
65 
66         std::threadpool pool(4);
67         std::vector< std::future<int> > results;
68 
69         for (int i = 0; i < 8; ++i) {
70             results.emplace_back(
71                 pool.commit([i] {
72                     std::cout << "hello " << i << std::endl;
73                     std::this_thread::sleep_for(std::chrono::seconds(1));
74                     std::cout << "world " << i << std::endl;
75                     return i*i;
76                 })
77             );
78         }
79         std::cout << " =======  commit all2 ========= " << std::this_thread::get_id() << std::endl;
80 
81         for (auto && result : results)
82             std::cout << result.get() << ' ';
83         std::cout << std::endl;
84         return 0;
85     }
86 catch (std::exception& e) {
87     std::cout << "some unhappy happened...  " << std::this_thread::get_id() << e.what() << std::endl;
88 }
View Code

 

为了避嫌,先进行一下版权说明:代码是 me “写”的,但是思路来自 Internet, 特别是这个线程池实现(基本 copy 了这个实现,加上这位同学的实现和解释,好东西值得 copy ! 然后综合更改了下,更加简洁)。

实现原理

接着前面的废话说。“管理一个任务队列,一个线程队列,然后每次取一个任务分配给一个线程去做,循环往复。” 这个思路有神马问题?线程池一般要复用线程,所以如果是取一个 task 分配给某一个 thread,执行完之后再重新分配,在语言层面基本都是不支持的:一般语言的 thread 都是执行一个固定的 task 函数,执行完毕线程也就结束了(至少 c++ 是这样)。so 要如何实现 task 和 thread 的分配呢?

让每一个 thread 都去执行调度函数:循环获取一个 task,然后执行之。

idea 是不是很赞!保证了 thread 函数的唯一性,而且复用线程执行 task 。

即使理解了 idea,代码还是需要详细解释一下的。

  1. 一个线程 pool,一个任务队列 queue ,应该没有意见;
  2. 任务队列是典型的生产者-消费者模型,本模型至少需要两个工具:一个 mutex + 一个条件变量,或是一个 mutex + 一个信号量。mutex 实际上就是锁,保证任务的添加和移除(获取)的互斥性,一个条件变量是保证获取 task 的同步性:一个 empty 的队列,线程应该等待(阻塞);
  3. atomic<bool> 本身是原子类型,从名字上就懂:它们的操作 load()/store() 是原子操作,所以不需要再加 mutex。

c++语言细节

即使懂原理也不代表能写出程序,上面用了众多c++11的“奇技淫巧”,下面简单描述之。

  1. using Task = function<void()> 是类型别名,简化了 typedef 的用法。function<void()> 可以认为是一个函数类型,接受任意原型是 void() 的函数,或是函数对象,或是匿名函数。void() 意思是不带参数,没有返回值。
  2. pool.emplace_back([this]{...}) 和 pool.push_back([this]{...}) 功能一样,只不过前者性能会更好;
  3. pool.emplace_back([this]{...}) 是构造了一个线程对象,执行函数是拉姆达匿名函数 ;
  4. 所有对象的初始化方式均采用了 {},而不再使用 () 方式,因为风格不够一致且容易出错;
  5. 匿名函数: [this]{...} 不多说。[] 是捕捉器,this 是引用域外的变量 this指针, 内部使用死循环, 由cv_task.wait(lock,[this]{...}) 来阻塞线程;
  6. delctype(expr) 用来推断 expr 的类型,和 auto 是类似的,相当于类型占位符,占据一个类型的位置;auto f(A a, B b) -> decltype(a+b) 是一种用法,不能写作 decltype(a+b) f(A a, B b),为啥?! c++ 就是这么规定的!
  7. commit 方法是不是略奇葩!可以带任意多的参数,第一个参数是 f,后面依次是函数 f 的参数!(注意:参数要传struct/class的话,建议用pointer,小心变量的作用域) 可变参数模板是 c++11 的一大亮点,够亮!至于为什么是 Arg... 和 arg... ,因为规定就是这么用的!
  8. commit 直接使用只能调用stdcall函数,但有两种方法可以实现调用类成员,一种是使用   bind: .commit(std::bind(&Dog::sayHello, &dog)); 一种是用 mem_fn: .commit(std::mem_fn(&Dog::sayHello), &dog);
  9. make_shared 用来构造 shared_ptr 智能指针。用法大体是 shared_ptr<int> p = make_shared<int>(4) 然后 *p == 4 。智能指针的好处就是, 自动 delete !
  10. bind 函数,接受函数 f 和部分参数,返回currying后的匿名函数,譬如 bind(add, 4) 可以实现类似 add4 的函数!
  11. forward() 函数,类似于 move() 函数,后者是将参数右值化,前者是... 肿么说呢?大概意思就是:不改变最初传入的类型的引用类型(左值还是左值,右值还是右值);
  12. packaged_task 就是任务函数的封装类,通过 get_future 获取 future , 然后通过 future 可以获取函数的返回值(future.get());packaged_task 本身可以像函数一样调用 () ;
  13. queue 是队列类, front() 获取头部元素, pop() 移除头部元素;back() 获取尾部元素,push() 尾部添加元素;
  14. lock_guard 是 mutex 的 stack 封装类,构造的时候 lock(),析构的时候 unlock(),是 c++ RAII 的 idea;
  15. condition_variable cv; 条件变量, 需要配合 unique_lock 使用;unique_lock 相比 lock_guard 的好处是:可以随时 unlock() 和 lock()。 cv.wait() 之前需要持有 mutex,wait 本身会 unlock() mutex,如果条件满足则会重新持有 mutex。
  16. 最后线程池析构的时候,join() 可以等待任务都执行完在结束,很安全! 

Git

 代码保存在git,这里可以获取最新代码: https://github.com/lzpong/threadpool

 

 

 

[copy right from url: http://blog.csdn.net/zdarks/article/details/46994607, https://github.com/progschj/ThreadPool/blob/master/ThreadPool.h]

posted @ 2017-02-14 15:54  _Ong  阅读(132360)  评论(31编辑  收藏  举报