NeoPZ
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
TPZThreadPool Class Reference

Administers tasks that will be executed asynchronously. More...

#include <TPZThreadPool.h>

Collaboration diagram for TPZThreadPool:
[legend]

Public Member Functions

std::shared_future< void > run (const int priority, TPZAutoPointer< std::packaged_task< void(void) > > &task, TPZTaskGroup *taskGroup=NULL)
 submits a task to be executed by TPZThreadPool More...
 
void run (TPZAutoPointer< TPZReschedulableTask > &task)
 
void reschedule (const int priority, TPZAutoPointer< TPZReschedulableTask > &task)
 
std::shared_future< void > runNow (TPZAutoPointer< TPZReschedulableTask > &task)
 
template<typename... Args>
std::shared_future< void > run (const int priority, TPZTaskGroup *taskGroup, std::function< void(Args...) > func, Args... args)
 
template<typename... Args>
TPZAutoPointer< TPZReschedulableTaskrunReschedulable (const int priority, TPZTaskGroup *taskGroup, std::function< void(Args...) > func, Args... args)
 
void SetNumThreads (const unsigned numThreads)
 sets the number of threads to be executed simultaneously More...
 
int maxPriority () const
 
int minPriority () const
 
int threadCount () const
 

Static Public Member Functions

static TPZThreadPoolglobalInstance ()
 

Private Member Functions

 TPZThreadPool ()
 
int ActualThreadCount () const
 
void threadsLoop ()
 
void updatePriorities ()
 
void appendTaskToQueue (TPZAutoPointer< TPZTask > &task)
 
TPZAutoPointer< TPZTaskappendTaskToQueue (const int priority, TPZAutoPointer< std::packaged_task< void(void) >> &task, const bool system_task, TPZTaskGroup *taskGroup=NULL)
 
void checkForMaxAndMinPriority (const int priority)
 
 ~TPZThreadPool ()
 
template<typename... Args>
std::shared_future< void > runSystemTask (const int priority, std::function< void(Args...) > func, Args... args)
 Submits and processes a "maximum priority" system task. More...
 

Private Attributes

std::vector< std::thread > mThreads
 vector of thread objects More...
 
std::mutex mThreadsMutex
 one mutex to synchronize access to the data structures More...
 
unsigned int mThreadsToDelete
 
unsigned int mZombieThreads
 
bool mStop
 
TPZPriorityQueue< TPZAutoPointer< TPZTask >, std::vector< TPZAutoPointer< TPZTask > >, TPZTaskOrderingmTasksQueue
 
std::condition_variable mTaskAvailableCond
 
int mMinPriority
 
int mMaxPriority
 

Detailed Description

Administers tasks that will be executed asynchronously.

Definition at line 23 of file TPZThreadPool.h.

Constructor & Destructor Documentation

◆ TPZThreadPool()

TPZThreadPool::TPZThreadPool ( )
private

Definition at line 105 of file TPZThreadPool.cpp.

References appendTaskToQueue(), TPZTask::priority(), run(), and threadCount().

Referenced by runReschedulable().

◆ ~TPZThreadPool()

TPZThreadPool::~TPZThreadPool ( )
private

Member Function Documentation

◆ ActualThreadCount()

int TPZThreadPool::ActualThreadCount ( ) const
private

Definition at line 159 of file TPZThreadPool.cpp.

References mThreads.

Referenced by runReschedulable(), threadCount(), and threadsLoop().

◆ appendTaskToQueue() [1/2]

void TPZThreadPool::appendTaskToQueue ( TPZAutoPointer< TPZTask > &  task)
private

◆ appendTaskToQueue() [2/2]

TPZAutoPointer<TPZTask> TPZThreadPool::appendTaskToQueue ( const int  priority,
TPZAutoPointer< std::packaged_task< void(void) >> &  task,
const bool  system_task,
TPZTaskGroup taskGroup = NULL 
)
private

◆ checkForMaxAndMinPriority()

void TPZThreadPool::checkForMaxAndMinPriority ( const int  priority)
private

Definition at line 176 of file TPZThreadPool.cpp.

References mMaxPriority, mMinPriority, and TPZTask::priority().

Referenced by run(), runReschedulable(), and runSystemTask().

◆ globalInstance()

TPZThreadPool & TPZThreadPool::globalInstance ( )
static
Returns
a reference to the one and only instance of TPZThreadPool

Definition at line 238 of file TPZThreadPool.cpp.

Referenced by TPZRenumbering::ColorElements(), TPZStructMatrixGCTP::MultiThread_Assemble(), runNow(), and TPZStructMatrixBase::TPZStructMatrixBase().

◆ maxPriority()

int TPZThreadPool::maxPriority ( ) const

Definition at line 147 of file TPZThreadPool.cpp.

References mMaxPriority.

Referenced by runReschedulable().

◆ minPriority()

int TPZThreadPool::minPriority ( ) const

Definition at line 151 of file TPZThreadPool.cpp.

References mMinPriority.

Referenced by runReschedulable().

◆ reschedule()

void TPZThreadPool::reschedule ( const int  priority,
TPZAutoPointer< TPZReschedulableTask > &  task 
)

◆ run() [1/3]

std::shared_future<void> TPZThreadPool::run ( const int  priority,
TPZAutoPointer< std::packaged_task< void(void) > > &  task,
TPZTaskGroup taskGroup = NULL 
)

◆ run() [2/3]

void TPZThreadPool::run ( TPZAutoPointer< TPZReschedulableTask > &  task)

◆ run() [3/3]

template<typename... Args>
std::shared_future<void> TPZThreadPool::run ( const int  priority,
TPZTaskGroup taskGroup,
std::function< void(Args...) >  func,
Args...  args 
)
inline

Definition at line 38 of file TPZThreadPool.h.

References checkForMaxAndMinPriority(), and run().

◆ runNow()

std::shared_future< void > TPZThreadPool::runNow ( TPZAutoPointer< TPZReschedulableTask > &  task)

◆ runReschedulable()

template<typename... Args>
TPZAutoPointer<TPZReschedulableTask> TPZThreadPool::runReschedulable ( const int  priority,
TPZTaskGroup taskGroup,
std::function< void(Args...) >  func,
Args...  args 
)
inline

◆ runSystemTask()

template<typename... Args>
std::shared_future<void> TPZThreadPool::runSystemTask ( const int  priority,
std::function< void(Args...) >  func,
Args...  args 
)
inlineprivate

Submits and processes a "maximum priority" system task.

Definition at line 74 of file TPZThreadPool.h.

References appendTaskToQueue(), and checkForMaxAndMinPriority().

Referenced by threadsLoop().

◆ SetNumThreads()

void TPZThreadPool::SetNumThreads ( const unsigned  numThreads)

sets the number of threads to be executed simultaneously

Definition at line 87 of file TPZThreadPool.cpp.

References mTaskAvailableCond, mThreads, mThreadsMutex, mThreadsToDelete, threadCount(), and threadsLoop().

Referenced by runReschedulable().

◆ threadCount()

int TPZThreadPool::threadCount ( ) const
Returns
the number of threads currently available

Definition at line 155 of file TPZThreadPool.cpp.

References ActualThreadCount(), mThreadsToDelete, and mZombieThreads.

Referenced by TPZRenumbering::ColorElements(), run(), runNow(), runReschedulable(), SetNumThreads(), and TPZThreadPool().

◆ threadsLoop()

void TPZThreadPool::threadsLoop ( )
private

◆ updatePriorities()

void TPZThreadPool::updatePriorities ( )
private

Member Data Documentation

◆ mMaxPriority

int TPZThreadPool::mMaxPriority
private

Definition at line 92 of file TPZThreadPool.h.

Referenced by checkForMaxAndMinPriority(), maxPriority(), and updatePriorities().

◆ mMinPriority

int TPZThreadPool::mMinPriority
private

Definition at line 91 of file TPZThreadPool.h.

Referenced by checkForMaxAndMinPriority(), minPriority(), and updatePriorities().

◆ mStop

bool TPZThreadPool::mStop
private

Definition at line 88 of file TPZThreadPool.h.

Referenced by threadsLoop(), and ~TPZThreadPool().

◆ mTaskAvailableCond

std::condition_variable TPZThreadPool::mTaskAvailableCond
private

Definition at line 90 of file TPZThreadPool.h.

Referenced by appendTaskToQueue(), SetNumThreads(), threadsLoop(), and ~TPZThreadPool().

◆ mTasksQueue

TPZPriorityQueue<TPZAutoPointer<TPZTask>, std::vector<TPZAutoPointer<TPZTask> >, TPZTaskOrdering> TPZThreadPool::mTasksQueue
private

◆ mThreads

std::vector<std::thread> TPZThreadPool::mThreads
private

vector of thread objects

Definition at line 83 of file TPZThreadPool.h.

Referenced by ActualThreadCount(), SetNumThreads(), threadsLoop(), and ~TPZThreadPool().

◆ mThreadsMutex

std::mutex TPZThreadPool::mThreadsMutex
private

one mutex to synchronize access to the data structures

Definition at line 85 of file TPZThreadPool.h.

Referenced by SetNumThreads(), threadsLoop(), and ~TPZThreadPool().

◆ mThreadsToDelete

unsigned int TPZThreadPool::mThreadsToDelete
private

Definition at line 86 of file TPZThreadPool.h.

Referenced by SetNumThreads(), threadCount(), and threadsLoop().

◆ mZombieThreads

unsigned int TPZThreadPool::mZombieThreads
private

Definition at line 87 of file TPZThreadPool.h.

Referenced by threadCount(), and threadsLoop().


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