C++11以前咱们使用线程须要系统提供API、posix线程库或者使用boost提供的线程库,C++11后就加入了跨平台的线程类std::thread,线程同步相关类std::mutex、std::lock_guard、std::condition_variable、std::atomic以及异步操做相关类std::async、std::future、std::promise等等,这使得咱们编写跨平台的多线程程序变得容易,线程的一个高级应用就是线程池,使用线程池能够充分利用多核CPU的并行计算能力,以及避免了使用单个线程的建立和销毁的开销,因此线程池在实际项目中用的很普遍,不少RPC框架都是用了线程池来处理事务,好比说Thrift,easyrpc等等,接下来咱们将使用C++11来实现一个通用的半同步半异步线程池(我的博客也发表了《使用C++11实现一个半同步半异步线程池》)。ios
一个半同步半异步线程池分为三层。git
这三个层次之间须要使用std::mutex、std::condition_variable来进行事件同步,线程池的实现代码以下。github
#ifndef _THREADPOOL_H #define _THREADPOOL_H #include <vector> #include <queue> #include <thread> #include <mutex> #include <memory> #include <functional> #include <condition_variable> #include <atomic> #include <type_traits> static const std::size_t max_task_quque_size = 100000; static const std::size_t max_thread_size = 30; class thread_pool { public: using work_thread_ptr = std::shared_ptr<std::thread>; using task_t = std::function<void()>; explicit thread_pool() : _is_stop_threadpool(false) {} ~thread_pool() { stop(); } void init_thread_num(std::size_t num) { if (num <= 0 || num > max_thread_size) { std::string str = "Number of threads in the range of 1 to " + std::to_string(max_thread_size); throw std::invalid_argument(str); } for (std::size_t i = 0; i < num; ++i) { work_thread_ptr t = std::make_shared<std::thread>(std::bind(&thread_pool::run_task, this)); _thread_vec.emplace_back(t); } } // 支持普通全局函数、静态函数、以及lambda表达式 template<typename Function, typename... Args> void add_task(const Function& func, Args... args) { if (!_is_stop_threadpool) { // 用lambda表达式来保存函数地址和参数 task_t task = [&func, args...]{ return func(args...); }; add_task_impl(task); } } // 支持函数对象(仿函数) template<typename Function, typename... Args> typename std::enable_if<std::is_class<Function>::value>::type add_task(Function& func, Args... args) { if (!_is_stop_threadpool) { task_t task = [&func, args...]{ return func(args...); }; add_task_impl(task); } } // 支持类成员函数 template<typename Function, typename Self, typename... Args> void add_task(const Function& func, Self* self, Args... args) { if (!_is_stop_threadpool) { task_t task = [&func, &self, args...]{ return (*self.*func)(args...); }; add_task_impl(task); } } void stop() { // 保证terminate_all函数只被调用一次 std::call_once(_call_flag, [this]{ terminate_all(); }); } private: void add_task_impl(const task_t& task) { { // 任务队列满了将等待线程池消费任务队列 std::unique_lock<std::mutex> locker(_task_queue_mutex); while (_task_queue.size() == max_task_quque_size && !_is_stop_threadpool) { _task_put.wait(locker); } _task_queue.emplace(std::move(task)); } // 向任务队列插入了一个任务并提示线程池能够来取任务了 _task_get.notify_one(); } void terminate_all() { _is_stop_threadpool = true; _task_get.notify_all(); for (auto& iter : _thread_vec) { if (iter != nullptr) { if (iter->joinable()) { iter->join(); } } } _thread_vec.clear(); clean_task_queue(); } void run_task() { // 线程池循环取任务 while (true) { task_t task = nullptr; { // 任务队列为空将等待 std::unique_lock<std::mutex> locker(_task_queue_mutex); while (_task_queue.empty() && !_is_stop_threadpool) { _task_get.wait(locker); } if (_is_stop_threadpool) { break; } if (!_task_queue.empty()) { task = std::move(_task_queue.front()); _task_queue.pop(); } } if (task != nullptr) { // 执行任务,并通知同步服务层能够向队列听任务了 task(); _task_put.notify_one(); } } } void clean_task_queue() { std::lock_guard<std::mutex> locker(_task_queue_mutex); while (!_task_queue.empty()) { _task_queue.pop(); } } private: std::vector<work_thread_ptr> _thread_vec; std::condition_variable _task_put; std::condition_variable _task_get; std::mutex _task_queue_mutex; std::queue<task_t> _task_queue; std::atomic<bool> _is_stop_threadpool; std::once_flag _call_flag; }; #endif
#include <iostream> #include <string> #include <chrono> #include "thread_pool.hpp" void test_task(const std::string& str) { std::cout << "Current thread id: " << std::this_thread::get_id() << ", str: " << str << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(50)); } class Test { public: void print(const std::string& str, int i) { std::cout << "Test: " << str << ", i: " << i << std::endl; } }; class Test2 { public: void operator()(const std::string& str, int i) { std::cout << "Test2: " << str << ", i: " << i << std::endl; } }; int main() { Test t; Test2 t2; thread_pool pool; // 启动10个线程 pool.init_thread_num(10); std::string str = "Hello world"; for (int i = 0; i < 1000; ++i) { // 支持lambda表达式 pool.add_task([]{ std::cout << "Hello ThreadPool" << std::endl; }); // 支持全局函数 pool.add_task(test_task, str); // 支持函数对象 pool.add_task(t2, str, i); // 支持类成员函数 pool.add_task(&Test::print, &t, str, i); } std::cin.get(); std::cout << "##############END###################" << std::endl; return 0; }
测试程序启动了十个线程并调用add_task函数加入了4000个任务,add_task支持普通全局函数、静态函数、类成员函数、函数对象(仿函数)以及lambda表达式,而且支持函数传入,该线程池的实现以及测试代码我已经放到了github上。apache
《深刻应用C++11--代码优化与工程级应用》promise