52#include <condition_variable>
79 assert(max_threads>0);
94 unsigned int go(
const std::function<
void()> &threadfunc)
99 notify(
"Max allowed threads running, waiting for one to end...");
102 unsigned int free_tidx=0;
110 if(
threads[free_tidx].m_thread)
112 notify(
"Joining past ",free_tidx,
"th thread before lanching new one.");
113 threads[free_tidx].m_thread->join();
118 notify(
"Thread tid=",
threads[free_tidx].tid,
" (",free_tidx,
"th) launched...");
128 notify(
"Calling join_all...");
141 notify(
"join_all: Joining ",i,
"th thread.");
151 notify(
"join_all finished...");
164 void launch_thread(
const std::function<
void()> &threadfunc,
unsigned int threadsid)
166 notify(
"Starting launch_thread(tid=",
threads[threadsid].tid,
", ",threadsid,
"th) ...");
168 notify(
"Finishing launch_thread(tid=",
threads[threadsid].tid,
", ",threadsid,
"th).");
190 static std::atomic<unsigned int>&
tid_counter() {
static std::atomic<unsigned int> c(0);
return c;}
~ThreadPool()
Joins every in-flight job before destroying the pool.
Definition thread_pool.hpp:83
unsigned int get_max_threads() const
returns the pool capacity (max_threads as given to the constructor)
Definition thread_pool.hpp:85
ThreadPool(const unsigned int max_threads=2)
Constructs a pool that allows up to max_threads concurrently running worker threads.
Definition thread_pool.hpp:77
unsigned int go(const std::function< void()> &threadfunc)
Launches threadfunc in a free pool slot, blocking the caller until one becomes available if the pool ...
Definition thread_pool.hpp:94
const unsigned int _max_threads
pool capacity, fixed at construction
Definition thread_pool.hpp:185
std::condition_variable cond_launcher
signaled by launch_thread() when a slot frees up, to wake a go() call waiting for capacity
Definition thread_pool.hpp:194
static std::atomic< unsigned int > & tid_counter()
process-wide tid sequence for notify() diagnostics (function-local static avoids header ODR issues)
Definition thread_pool.hpp:190
std::unique_ptr< std::thread > ThreadPtr
Definition thread_pool.hpp:179
std::vector< ThreadsArray > threads
one slot per allowed concurrent thread; sized once in the constructor, never resized (no reallocation...
Definition thread_pool.hpp:186
std::mutex mutex_launcher
guards threads[], _running_threads and slot bookkeeping in go()/join_all()/launch_thread()
Definition thread_pool.hpp:193
unsigned int get_running_threads() const
Returns the number of pool slots with an active thread right now. The counter is read without taking ...
Definition thread_pool.hpp:157
void launch_thread(const std::function< void()> &threadfunc, unsigned int threadsid)
Worker-thread trampoline: runs threadfunc to completion, then marks slot threadsid free and wakes one...
Definition thread_pool.hpp:164
void join_all()
Joins every currently running worker thread and resets the pool to empty (all slots free)....
Definition thread_pool.hpp:126
unsigned int _running_threads
current count of slots with an active thread (guarded by mutex_launcher)
Definition thread_pool.hpp:191
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
Structure to keep status of threads in the pool.
Definition thread_pool.hpp:181
int tid
monotonically increasing id assigned when a thread is launched into this slot, -1 if the slot is free
Definition thread_pool.hpp:183
ThreadPtr m_thread
owns the worker thread's std::thread handle, or null if this slot is free
Definition thread_pool.hpp:182