#pragma once #include #include #include #include #include class ThreadPool { private: std::condition_variable m_cond_var; bool m_destruct_pool; std::mutex m_mutex; std::vector m_threads; std::queue> m_work_queue; public: ThreadPool(unsigned int initial_pool_size); ~ThreadPool(); template inline auto SubmitWork(F&& f, Args&&... args) -> std::future { auto func = std::bind(std::forward(f), std::forward(args)...); auto task = std::make_shared>(std::forward(func)); { std::lock_guard lock(m_mutex); // wrap packed task into a void return function type so that it can be stored in queue m_work_queue.push([task]() { (*task)(); }); } m_cond_var.notify_one(); // unblocks one of the waiting threads return task->get_future(); } };