开启线程池
| 34 | |
| 35 | // 开启线程池 |
| 36 | void startPool(int size) |
| 37 | { |
| 38 | for (int i = 0; i < size; ++ i) { |
| 39 | // Thread 参数是一个函数对象,可以使用 bind 绑定 |
| 40 | _pool.push_back(new Thread(bind(&ThreadPool::runInThread, this, std::placeholders::_1), i)); |
| 41 | } |
| 42 | |
| 43 | for (int i = 0; i < size; ++ i) { |
| 44 | _handler.push_back(_pool[i]->start()); |
| 45 | } |
| 46 | |
| 47 | for (thread &t: _handler) { |
| 48 | t.join(); |
| 49 | } |
| 50 | } |
| 51 | private: |
| 52 | vector<Thread *> _pool; |
| 53 | vector<thread> _handler; |