#ifndef THREAD_POOL_H #define THREAD_POOL_H #include #include #include #include #include #include #include #include #include class ThreadPool { public: ThreadPool(); ~ThreadPool(); ThreadPool(const ThreadPool&) = delete; ThreadPool(ThreadPool&&) = delete; ThreadPool& operator=(const ThreadPool&) = delete; ThreadPool& operator=(ThreadPool&&) = delete; template auto enqueue(F&& f, Args&&... args) -> std::future { auto task = std::bind(std::forward(f), std::forward(args)...); auto taskPtr = std::make_shared>(task); auto wrapper = [taskPtr]() { (*taskPtr)(); }; { std::lock_guard lock(m_mutex); m_tasks.push(wrapper); m_condition_task.notify_one(); } return taskPtr->get_future(); } void wait(); private: mutable std::mutex m_mutex; std::condition_variable m_condition_task; std::condition_variable m_condition_finish; unsigned int m_busyThreads; std::vector m_threads; bool m_stopRequested; std::queue> m_tasks; friend class ThreadWorker; }; class ThreadWorker { public: ThreadWorker(ThreadPool* pool); ~ThreadWorker(); void operator()(); private: ThreadPool* m_pool; }; #endif // !THREAD_POOL_H