11 #ifndef TLX_THREAD_POOL_HEADER 12 #define TLX_THREAD_POOL_HEADER 16 #include <condition_variable> 87 std::atomic<size_t>
busy_ = { 0 };
89 std::atomic<size_t>
idle_ = { 0 };
91 std::atomic<size_t>
done_ = { 0 };
102 size_t num_threads = std::thread::hardware_concurrency(),
141 std::thread&
thread(
size_t i);
150 #endif // !TLX_THREAD_POOL_HEADER Delegate< void(size_t)> InitThread
std::atomic< size_t > done_
Counter for total number of jobs executed.
std::deque< Job > jobs_
Deque of scheduled jobs.
simple_vector< std::thread > threads_
threads in pool
size_t done() const
Return number of jobs currently completed.
size_t size() const
Return number of threads in pool.
Simpler non-growing vector without initialization.
std::atomic< bool > terminate_
Flag whether to terminate.
std::condition_variable cv_jobs_
void worker(size_t p)
Worker function, one per thread is started.
~ThreadPool()
Stop processing jobs, terminate threads.
ThreadPool(size_t num_threads=std::thread::hardware_concurrency(), InitThread &&init_thread=InitThread())
Construct running thread pool of num_threads.
void enqueue(Job &&job)
enqueue a Job, the caller must pass in all context using captures.
std::thread & thread(size_t i)
Return thread handle to thread i.
bool has_idle() const
true if any thread is idle (= waiting for jobs)
std::atomic< size_t > busy_
Counter for number of threads busy.
ThreadPool & operator=(const ThreadPool &)=delete
non-copyable: delete assignment operator
std::atomic< size_t > idle_
Counter for number of idle threads waiting for a job.
std::condition_variable cv_finished_
Condition variable to signal when a jobs finishes.
void loop_until_terminate()
Loop until terminate flag was set.
ThreadPool starts a fixed number p of std::threads which process Jobs that are enqueued into a concur...
size_t idle() const
return number of idle threads in pool
std::mutex mutex_
Mutex used to access the queue of scheduled jobs.
InitThread init_thread_
Run once per worker thread.