Implements thread scheduler that assigns jobs up to maximum number of threads allowed.
More...
#include <thread_pool.hpp>
|
| | ThreadPool (const unsigned int max_threads=2) |
| | Constructs a pool that allows up to max_threads concurrently running worker threads.
|
|
| ~ThreadPool () |
| | Joins every in-flight job before destroying the pool.
|
|
unsigned int | get_max_threads () const |
| | returns the pool capacity (max_threads as given to the constructor)
|
| 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 is currently at capacity. Any previous thread that occupied the chosen slot is joined first (a slot's std::thread handle is reused only after its predecessor has finished and been joined).
|
|
void | join_all () |
| | Joins every currently running worker thread and resets the pool to empty (all slots free). Safe to call from the destructor or explicitly mid-lifetime to synchronize with in-flight jobs.
|
|
unsigned int | get_running_threads () const |
| | Returns the number of pool slots with an active thread right now. The counter is read without taking the pool mutex, so the value is a snapshot that may already be stale when it is returned; use it for reporting, never to make a decision.
|
|
| typedef std::unique_ptr< std::thread > | ThreadPtr |
|
| 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 waiter in go().
|
|
|
static std::atomic< unsigned int > & | tid_counter () |
| | process-wide tid sequence for notify() diagnostics (function-local static avoids header ODR issues)
|
|
|
const unsigned int | _max_threads |
| | pool capacity, fixed at construction
|
|
std::vector< ThreadsArray > | threads |
| | one slot per allowed concurrent thread; sized once in the constructor, never resized (no reallocation races)
|
|
unsigned int | _running_threads |
| | current count of slots with an active thread (guarded by mutex_launcher)
|
|
std::mutex | mutex_launcher |
| | guards threads[], _running_threads and slot bookkeeping in go()/join_all()/launch_thread()
|
|
std::condition_variable | cond_launcher |
| | signaled by launch_thread() when a slot frees up, to wake a go() call waiting for capacity
|
Implements thread scheduler that assigns jobs up to maximum number of threads allowed.
The capacity is a runtime constructor argument, so the same binary can be driven with any thread count. The pool is deliberately minimal: go() launches a job and blocks the caller while the pool is at capacity, and the only synchronization primitive offered is join_all(), which waits for every in-flight job. There is no per-job handle, no future and no result channel – a job communicates by writing into structures the caller owns and outlives it.
- Warning
- An exception escaping a job function propagates out of the worker thread and terminates the process (std::terminate). A job must catch everything it can throw.
◆ ThreadPtr
owning handle to a pool worker's std::thread
◆ ThreadPool()
| FST::ThreadPool::ThreadPool |
( |
const unsigned int | max_threads = 2 | ) |
|
|
inlineexplicit |
Constructs a pool that allows up to max_threads concurrently running worker threads.
- Parameters
-
| max_threads | pool capacity; must be greater than zero (asserted) |
◆ go()
| unsigned int FST::ThreadPool::go |
( |
const std::function< void()> & | threadfunc | ) |
|
|
inline |
Launches threadfunc in a free pool slot, blocking the caller until one becomes available if the pool is currently at capacity. Any previous thread that occupied the chosen slot is joined first (a slot's std::thread handle is reused only after its predecessor has finished and been joined).
- Parameters
-
| threadfunc | the job to run on the worker thread |
- Returns
- the 0-based slot index the job was launched into. This is a diagnostic value, not a job handle: the slot is recycled as soon as the job ends and there is no way to wait on that one job. Use join_all() to synchronize with all outstanding jobs.
◆ launch_thread()
| void FST::ThreadPool::launch_thread |
( |
const std::function< void()> & | threadfunc, |
|
|
unsigned int | threadsid ) |
|
inlineprivate |
Worker-thread trampoline: runs threadfunc to completion, then marks slot threadsid free and wakes one waiter in go().
- Parameters
-
| threadfunc | the job to execute |
| threadsid | the pool slot this worker occupies (re-entrant across concurrently running workers) |
The documentation for this class was generated from the following file: