|
VR-Engage
2.2
|
This class provides a pool of worker threads that can execute jobs asynchronously. It manages thread creation, job submission, and thread synchronization. Jobs can be submitted as functions with or without arguments, and the results can be retrieved using futures.
#include <vreThreadPool.h>
Public Types | |
| using | JobType = std::function<void()> |
Public Member Functions | |
| DtVreThreadPool (std::size_t threads) | |
| ~DtVreThreadPool () | |
| DtVreThreadPool (const DtVreThreadPool &)=delete | |
| DtVreThreadPool (DtVreThreadPool &&)=delete | |
| DtVreThreadPool & | operator= (const DtVreThreadPool &)=delete |
| DtVreThreadPool & | operator= (DtVreThreadPool &&)=delete |
| void | shutdown (bool join=true) |
| std::size_t | threadCount () const |
| void | resize (std::size_t nThreads) |
| std::size_t | activeCount () const |
| template<typename F, typename... Args, typename R = typename std::result_of<F(Args...)>::type> | |
| std::future< R > | submitJob (F &&f, Args &&... args) |
| template<typename F, typename R = typename std::result_of<F()>::type> | |
| std::future< R > | submitJob (F &&f) |
Static Public Member Functions | |
| template<typename R> | |
| static bool | isFutureReady (std::future< R > const &f) |
| template<typename R> | |
| static std::future_status | getFutureStatus (std::future< R > const &f) |
| template<typename R> | |
| static bool | isSharedFutureReady (std::shared_future< R > const &f) |
| template<typename R> | |
| static std::future_status | getSharedFutureStatus (std::shared_future< R > const &f) |
Protected Member Functions | |
| JobType | getNextJob () |
| void | emptyQueue () |
| void | createThreads (std::size_t nThreads) |
Protected Attributes | |
| std::vector< std::unique_ptr< std::thread > > | myThreads |
| std::queue< JobType > | myJobQueue |
| std::atomic< bool > | myStop |
| std::atomic< std::size_t > | myActiveThreads |
| std::mutex | myMutex |
| std::condition_variable | myNotifier |
| using makVre::DtVreThreadPool::JobType = std::function<void()> |
Function type for jobs to be executed by the thread pool.
|
explicit |
Constructor that initializes the thread pool with a specified number of threads.
| threads | The number of worker threads to create in the pool |
Referenced by DtVreThreadPool(), DtVreThreadPool(), operator=(), and operator=().
| makVre::DtVreThreadPool::~DtVreThreadPool | ( | ) |
Destructor that shuts down the thread pool.
Calls shutdown(true) to ensure all threads are properly joined
|
delete |
Copy constructor (deleted)
References DtVreThreadPool().
|
delete |
Move constructor (deleted)
References DtVreThreadPool().
|
delete |
Copy assignment operator (deleted)
References DtVreThreadPool().
|
delete |
Move assignment operator (deleted)
References DtVreThreadPool().
| void makVre::DtVreThreadPool::shutdown | ( | bool | join = true | ) |
Shuts down the thread pool.
Stops all worker threads and optionally waits for them to complete their current tasks.
| join | If true, waits for all threads to finish; if false, detaches them |
| std::size_t makVre::DtVreThreadPool::threadCount | ( | ) | const |
Returns the number of threads in the pool.
| void makVre::DtVreThreadPool::resize | ( | std::size_t | nThreads | ) |
Resizes the thread pool to a new number of threads.
Shuts down the current pool and creates a new one with the specified number of threads
| nThreads | The new number of threads to use |
| std::size_t makVre::DtVreThreadPool::activeCount | ( | ) | const |
Returns the number of currently active (working) threads.
References getFutureStatus(), getSharedFutureStatus(), isFutureReady(), isSharedFutureReady(), and submitJob().
| std::future< R > makVre::DtVreThreadPool::submitJob | ( | F && | f, |
| Args &&... | args ) |
Submits a job with arguments to the thread pool.
| f | The function to execute |
| args | The arguments to pass to the function |
References myJobQueue, myMutex, and myNotifier.
Referenced by activeCount().
| std::future< R > makVre::DtVreThreadPool::submitJob | ( | F && | f | ) |
Submits a job without arguments to the thread pool.
| f | The function to execute |
References myJobQueue, myMutex, and myNotifier.
|
static |
Checks if a future is ready (completed)
| f | The future to check |
Referenced by activeCount().
|
static |
Gets the status of a future.
| f | The future to check |
Referenced by activeCount().
|
static |
Checks if a shared future is ready (completed)
| f | The shared future to check |
Referenced by activeCount().
|
static |
Gets the status of a shared future.
| f | The shared future to check |
Referenced by activeCount().
|
protected |
Gets the next job from the queue.
Waits until a job is available or the thread pool is stopped
|
protected |
Empties the job queue.
Removes all pending jobs from the queue
|
protected |
Creates worker threads for the pool.
| nThreads | The number of threads to create |
|
protected |
Collection of worker threads.
|
protected |
Queue of pending jobs.
Referenced by submitJob(), and submitJob().
|
protected |
Flag indicating if the thread pool should stop.
|
protected |
Counter for the number of active threads.
|
protected |
Mutex for synchronizing access to the job queue.
Referenced by submitJob(), and submitJob().
|
protected |
Condition variable for notifying threads of new jobs.
Referenced by submitJob(), and submitJob().