网上大佬用 C++11 写的线程池代码,和自己的阅读笔记
代码中有很多c11
新增的高级操作, 配合笔记 一起理解
ThreadPool
.h 头文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| #ifndef THREAD_POOL_H #define THREAD_POOL_H #include <thread> #include <mutex> #include <atomic> #include <condition_variable> #include <functional> #include <future> #include <queue> #include <type_traits> #include <utility> #include <vector>
namespace zl { class ThreadsGuard { public: ThreadsGuard(std::vector<std::thread>& v) : threads_(v) { } ~ThreadsGuard() { for (size_t i = 0; i != threads_.size(); ++i) { if (threads_[i].joinable()) { threads_[i].join(); } } } private: ThreadsGuard(ThreadsGuard&& tg) = delete; ThreadsGuard& operator = (ThreadsGuard&& tg) = delete;
ThreadsGuard(const ThreadsGuard&) = delete; ThreadsGuard& operator = (const ThreadsGuard&) = delete; private: std::vector<std::thread>& threads_; };
class ThreadPool { public: typedef std::function<void()> task_type;
public: explicit ThreadPool(int n = 0);
~ThreadPool() { stop(); cond_.notify_all(); }
void stop(){ stop_.store(true, std::memory_order_release); }
template<class Function, class... Args> std::future<typename std::result_of<Function(Args...)>::type> add(Function&&, Args&&...);
private: ThreadPool(ThreadPool&&) = delete; ThreadPool& operator = (ThreadPool&&) = delete; ThreadPool(const ThreadPool&) = delete; ThreadPool& operator = (const ThreadPool&) = delete;
private: std::atomic<bool> stop_; std::mutex mtx_; std::condition_variable cond_;
std::queue<task_type> tasks_; std::vector<std::thread> threads_; zl::ThreadsGuard tg_; };
inline ThreadPool::ThreadPool(int n) : stop_(false) , tg_(threads_) { int nthreads = n; if (nthreads <= 0) { nthreads = std::thread::hardware_concurrency(); nthreads = (nthreads == 0 ? 2 : nthreads); }
for (int i = 0; i != nthreads; ++i) { threads_.push_back(std::thread([this]{ while (!stop_.load(std::memory_order_acquire)) { task_type task; { std::unique_lock<std::mutex> ulk(this->mtx_); this->cond_.wait(ulk, [this]{ return stop_.load(std::memory_order_acquire) || !this->tasks_.empty(); }); if (stop_.load(std::memory_order_acquire)) return; task = std::move(this->tasks_.front()); this->tasks_.pop(); } task(); } })); } }
template<class Function, class... Args> std::future<typename std::result_of<Function(Args...)>::type> ThreadPool::add(Function&& fcn, Args&&... args) {
typedef typename std::result_of<Function(Args...)>::type return_type;
typedef std::packaged_task<return_type()> task;
auto t = std::make_shared<task>(std::bind(std::forward<Function>(fcn), std::forward<Args>(args)...));
auto ret = t->get_future(); {
std::lock_guard<std::mutex> lg(mtx_); if (stop_.load(std::memory_order_acquire)) throw std::runtime_error("thread pool has stopped");
tasks_.emplace([t]{(*t)(); }); } cond_.notify_one(); return ret; } }
#endif
|
main
文件使用例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <iostream> #include <string> #include "ThreadPool.h"
using namespace std;
int main(){ std::mutex mtx; try{ zl::ThreadPool tp; std::vector<std::future<int>> v; std::vector<std::future<void>> v1;
for (int i = 0; i <= 10; ++i) { auto ans = tp.add([](int answer) { return answer; }, i); v.push_back(std::move(ans)); }
for (int i = 0; i <= 5; ++i){ auto ans = tp.add([&mtx](const std::string& str1, const std::string& str2) { std::lock_guard<std::mutex> lg(mtx); std::cout << (str1 + str2) << std::endl; return; }, "hello ", "world"); v1.push_back(std::move(ans)); } for (size_t i = 0; i < v.size(); ++i) { std::lock_guard<std::mutex> lg(mtx); cout << v[i].get() << endl; } for (size_t i = 0; i < v1.size(); ++i){ v1[i].get(); } } catch (std::exception& e){ std::cout << e.what() << std::endl; }
}
|