#pragma once ///////////////////////////////////////////////////////////////////// // ThreadPool.h - Enqueue work items to run on one of a collection // // of threads, created at startup. // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2016 // ///////////////////////////////////////////////////////////////////// /* * Threadpool makes copy of each work item on the heap in case it * changes before executing. Copy is deleted after execution. * * This means that work items have to be copyable. */ #include #include #include #include #include #include "../Cpp11-BlockingQueue/Cpp11-BlockingQueue.h" class ThreadPool { public: using CallObj = std::function; ThreadPool(size_t n=8); ThreadPool(const ThreadPool&) = delete; ThreadPool& operator=(const ThreadPool&) = delete; void workItem(CallObj* pCo); void wait(); std::mutex& mutex(); ~ThreadPool(); private: BlockingQueue Q_; std::vector threads_; std::mutex mtx_; // lock shared by all threads in pool }; inline std::mutex& ThreadPool::mutex() { return mtx_; }