A pool of threads.
| 79 | |
| 80 | // A pool of threads. |
| 81 | class ThreadPool { |
| 82 | public: |
| 83 | // Default thread workers. |
| 84 | ThreadPool(size_t num_threads, |
| 85 | size_t maximum_queue_size = std::numeric_limits<size_t>::max(), |
| 86 | int core_offset = -1); |
| 87 | |
| 88 | // User-defined thread workers. |
| 89 | ThreadPool(std::vector<std::unique_ptr<Worker>> workers, |
| 90 | size_t maximum_queue_size = std::numeric_limits<size_t>::max(), |
| 91 | int core_offset = -1); |
| 92 | |
| 93 | ~ThreadPool(); |
| 94 | |
| 95 | // Posts a new job. The method blocks if the job queue is full. |
| 96 | void post(std::unique_ptr<Job> job); |
| 97 | |
| 98 | size_t num_threads() const; |
| 99 | |
| 100 | // Number of jobs in the queue. |
| 101 | size_t num_queued_jobs() const; |
| 102 | |
| 103 | // Number of jobs in the queue and currently processed by a worker. |
| 104 | size_t num_active_jobs() const; |
| 105 | |
| 106 | Worker& get_worker(size_t index); |
| 107 | static Worker& get_local_worker(); |
| 108 | |
| 109 | private: |
| 110 | void start_workers(int core_offset); |
| 111 | |
| 112 | JobQueue _queue; |
| 113 | std::vector<std::unique_ptr<Worker>> _workers; |
| 114 | std::atomic<size_t> _num_active_jobs; |
| 115 | }; |
| 116 | |
| 117 | } |