VR-Engage  2.2
Loading...
Searching...
No Matches
makVre::DtVreThreadPool Class Reference

Detailed Description

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
 
DtVreThreadPooloperator= (const DtVreThreadPool &)=delete
 
DtVreThreadPooloperator= (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< JobTypemyJobQueue
 
std::atomic< bool > myStop
 
std::atomic< std::size_t > myActiveThreads
 
std::mutex myMutex
 
std::condition_variable myNotifier
 

Member Typedef Documentation

◆ JobType

using makVre::DtVreThreadPool::JobType = std::function<void()>

Function type for jobs to be executed by the thread pool.

Constructor & Destructor Documentation

◆ DtVreThreadPool() [1/3]

makVre::DtVreThreadPool::DtVreThreadPool ( std::size_t threads)
explicit

Constructor that initializes the thread pool with a specified number of threads.

Parameters
threadsThe number of worker threads to create in the pool

Referenced by DtVreThreadPool(), DtVreThreadPool(), operator=(), and operator=().

◆ ~DtVreThreadPool()

makVre::DtVreThreadPool::~DtVreThreadPool ( )

Destructor that shuts down the thread pool.

Calls shutdown(true) to ensure all threads are properly joined

◆ DtVreThreadPool() [2/3]

makVre::DtVreThreadPool::DtVreThreadPool ( const DtVreThreadPool & )
delete

Copy constructor (deleted)

References DtVreThreadPool().

◆ DtVreThreadPool() [3/3]

makVre::DtVreThreadPool::DtVreThreadPool ( DtVreThreadPool && )
delete

Move constructor (deleted)

References DtVreThreadPool().

Member Function Documentation

◆ operator=() [1/2]

DtVreThreadPool & makVre::DtVreThreadPool::operator= ( const DtVreThreadPool & )
delete

Copy assignment operator (deleted)

References DtVreThreadPool().

◆ operator=() [2/2]

DtVreThreadPool & makVre::DtVreThreadPool::operator= ( DtVreThreadPool && )
delete

Move assignment operator (deleted)

References DtVreThreadPool().

◆ shutdown()

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.

Parameters
joinIf true, waits for all threads to finish; if false, detaches them

◆ threadCount()

std::size_t makVre::DtVreThreadPool::threadCount ( ) const

Returns the number of threads in the pool.

Returns
The total number of threads managed by this pool

◆ resize()

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

Parameters
nThreadsThe new number of threads to use

◆ activeCount()

std::size_t makVre::DtVreThreadPool::activeCount ( ) const

Returns the number of currently active (working) threads.

Returns
The number of threads currently executing jobs

References getFutureStatus(), getSharedFutureStatus(), isFutureReady(), isSharedFutureReady(), and submitJob().

◆ submitJob() [1/2]

template<typename F, typename... Args, typename R>
std::future< R > makVre::DtVreThreadPool::submitJob ( F && f,
Args &&... args )

Submits a job with arguments to the thread pool.

Parameters
fThe function to execute
argsThe arguments to pass to the function
Returns
A future that can be used to retrieve the result of the function

References myJobQueue, myMutex, and myNotifier.

Referenced by activeCount().

◆ submitJob() [2/2]

template<typename F, typename R>
std::future< R > makVre::DtVreThreadPool::submitJob ( F && f)

Submits a job without arguments to the thread pool.

Parameters
fThe function to execute
Returns
A future that can be used to retrieve the result of the function

References myJobQueue, myMutex, and myNotifier.

◆ isFutureReady()

template<typename R>
bool makVre::DtVreThreadPool::isFutureReady ( std::future< R > const & f)
static

Checks if a future is ready (completed)

Parameters
fThe future to check
Returns
true if the future is ready, false otherwise

Referenced by activeCount().

◆ getFutureStatus()

template<typename R>
std::future_status makVre::DtVreThreadPool::getFutureStatus ( std::future< R > const & f)
static

Gets the status of a future.

Parameters
fThe future to check
Returns
The status of the future (ready, timeout, or deferred)

Referenced by activeCount().

◆ isSharedFutureReady()

template<typename R>
bool makVre::DtVreThreadPool::isSharedFutureReady ( std::shared_future< R > const & f)
static

Checks if a shared future is ready (completed)

Parameters
fThe shared future to check
Returns
true if the shared future is ready, false otherwise

Referenced by activeCount().

◆ getSharedFutureStatus()

template<typename R>
std::future_status makVre::DtVreThreadPool::getSharedFutureStatus ( std::shared_future< R > const & f)
static

Gets the status of a shared future.

Parameters
fThe shared future to check
Returns
The status of the shared future (ready, timeout, or deferred)

Referenced by activeCount().

◆ getNextJob()

JobType makVre::DtVreThreadPool::getNextJob ( )
protected

Gets the next job from the queue.

Waits until a job is available or the thread pool is stopped

Returns
The next job to execute, or an empty function if the pool is stopped

◆ emptyQueue()

void makVre::DtVreThreadPool::emptyQueue ( )
protected

Empties the job queue.

Removes all pending jobs from the queue

◆ createThreads()

void makVre::DtVreThreadPool::createThreads ( std::size_t nThreads)
protected

Creates worker threads for the pool.

Parameters
nThreadsThe number of threads to create

Member Data Documentation

◆ myThreads

std::vector<std::unique_ptr<std::thread> > makVre::DtVreThreadPool::myThreads
protected

Collection of worker threads.

◆ myJobQueue

std::queue<JobType> makVre::DtVreThreadPool::myJobQueue
protected

Queue of pending jobs.

Referenced by submitJob(), and submitJob().

◆ myStop

std::atomic<bool> makVre::DtVreThreadPool::myStop
protected

Flag indicating if the thread pool should stop.

◆ myActiveThreads

std::atomic<std::size_t> makVre::DtVreThreadPool::myActiveThreads
protected

Counter for the number of active threads.

◆ myMutex

std::mutex makVre::DtVreThreadPool::myMutex
protected

Mutex for synchronizing access to the job queue.

Referenced by submitJob(), and submitJob().

◆ myNotifier

std::condition_variable makVre::DtVreThreadPool::myNotifier
protected

Condition variable for notifying threads of new jobs.

Referenced by submitJob(), and submitJob().


The documentation for this class was generated from the following file: