13 #ifndef META_THREAD_POOL_H_
14 #define META_THREAD_POOL_H_
16 #include <condition_variable>
40 thread_pool(
size_t num_threads = std::thread::hardware_concurrency())
43 for (
size_t i = 0; i < num_threads; ++i)
45 std::thread{std::bind(&thread_pool::worker, this)});
54 std::unique_lock<std::mutex> lock(
mutex_);
68 template <
class Function>
69 std::future<typename std::result_of<Function()>::type>
72 using result_type =
typename std::result_of<Function()>::type;
74 std::unique_ptr<concrete_task<result_type>>
task(
77 auto future = task->get_future();
79 std::unique_lock<std::mutex> lock(
mutex_);
80 tasks_.push(std::move(task));
91 std::vector<std::thread::id> ids;
93 ids.emplace_back(t.get_id());
102 std::unique_lock<std::mutex> lock(
mutex_);
115 virtual void run() = 0;
120 virtual ~task() =
default;
134 template <
class Function>
145 virtual void run()
override
155 return task_.get_future();
170 std::unique_ptr<task>
task;
172 std::unique_lock<std::mutex> lock(
mutex_);
177 task = std::move(
tasks_.front());
187 std::queue<std::unique_ptr<task>>
tasks_;