#ifndef THREAD_POOL_H_ #define THREAD_POOL_H_ #include #include #include #include #include class ThreadPool { public: // Initialize thread pool and start workers. explicit ThreadPool(int thread_count); // Cleanup workers. ~ThreadPool(); // Add a function to work queue. void Add(std::function &&func); private: // Take work off of work_queue_ and run it. void WorkerThread(); // Worker threads. std::vector workers_; // Work to be done. std::queue> work_queue_; // True if it's time for worker threads to exit. bool end_workers_ = false; // Condition to watch for work_queue_ and end_workers_ changes. std::condition_variable cv_; // One mutex for everything. std::mutex mu_; }; #endif // THREAD_POOL_H_