LibreOffice Module comphelper (master) 1
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
comphelper::ThreadPool Class Referencefinal

A very basic thread-safe thread pool implementation. More...

#include <threadpool.hxx>

Classes

class  ThreadWorker
 

Public Member Functions

 ThreadPool (std::size_t nWorkers)
 
 ~ThreadPool ()
 
void pushTask (std::unique_ptr< ThreadTask > pTask)
 push a new task onto the work queue More...
 
void waitUntilDone (const std::shared_ptr< ThreadTaskTag > &, bool bJoin=true)
 Wait until all queued tasks associated with the tag are completed. More...
 
void joinThreadsIfIdle ()
 join all threads if there are no tasks presently. More...
 
bool isIdle () const
 return true if there are no queued or worked-on tasks More...
 
sal_Int32 getWorkerCount () const
 return the number of live worker threads More...
 
void shutdown ()
 wait until all work is completed, then join all threads More...
 

Static Public Member Functions

static ThreadPoolgetSharedOptimalPool ()
 returns a pointer to a shared pool with optimal thread count for the CPU More...
 
static std::shared_ptr< ThreadTaskTagcreateThreadTaskTag ()
 
static bool isTaskTagDone (const std::shared_ptr< ThreadTaskTag > &)
 
static std::size_t getPreferredConcurrency ()
 returns a configurable max-concurrency limit to avoid spawning an unnecessarily large number of threads on high-core boxes. More...
 

Private Member Functions

 ThreadPool (const ThreadPool &)=delete
 
ThreadPooloperator= (const ThreadPool &)=delete
 
std::unique_ptr< ThreadTaskpopWorkLocked (std::unique_lock< std::mutex > &rGuard, bool bWait)
 Pop a work task. More...
 
void shutdownLocked (std::unique_lock< std::mutex > &)
 
void incBusyWorker ()
 
void decBusyWorker ()
 

Private Attributes

std::mutex maMutex
 signalled when all in-progress tasks are complete More...
 
std::condition_variable maTasksChanged
 
bool mbTerminate
 
std::size_t const mnMaxWorkers
 
std::size_t mnBusyWorkers
 
std::vector< std::unique_ptr< ThreadTask > > maTasks
 
std::vector< rtl::Reference< ThreadWorker > > maWorkers
 

Friends

class ThreadWorker
 

Detailed Description

A very basic thread-safe thread pool implementation.

Definition at line 44 of file threadpool.hxx.

Constructor & Destructor Documentation

◆ ThreadPool() [1/2]

comphelper::ThreadPool::ThreadPool ( std::size_t  nWorkers)

Definition at line 96 of file threadpool.cxx.

◆ ~ThreadPool()

comphelper::ThreadPool::~ThreadPool ( )

Definition at line 103 of file threadpool.cxx.

References maTasks, mbTerminate, and mnBusyWorkers.

◆ ThreadPool() [2/2]

comphelper::ThreadPool::ThreadPool ( const ThreadPool )
privatedelete

Member Function Documentation

◆ createThreadTaskTag()

std::shared_ptr< ThreadTaskTag > comphelper::ThreadPool::createThreadTaskTag ( )
static

Definition at line 301 of file threadpool.cxx.

◆ decBusyWorker()

void comphelper::ThreadPool::decBusyWorker ( )
private

Definition at line 258 of file threadpool.cxx.

References mnBusyWorkers.

Referenced by comphelper::ThreadPool::ThreadWorker::execute().

◆ getPreferredConcurrency()

std::size_t comphelper::ThreadPool::getPreferredConcurrency ( )
static

returns a configurable max-concurrency limit to avoid spawning an unnecessarily large number of threads on high-core boxes.

MAX_CONCURRENCY env. var. controls the cap.

Definition at line 134 of file threadpool.cxx.

◆ getSharedOptimalPool()

ThreadPool & comphelper::ThreadPool::getSharedOptimalPool ( )
static

returns a pointer to a shared pool with optimal thread count for the CPU

Definition at line 129 of file threadpool.cxx.

◆ getWorkerCount()

sal_Int32 comphelper::ThreadPool::getWorkerCount ( ) const
inline

return the number of live worker threads

Definition at line 79 of file threadpool.hxx.

◆ incBusyWorker()

void comphelper::ThreadPool::incBusyWorker ( )
private

Definition at line 253 of file threadpool.cxx.

References mnBusyWorkers.

Referenced by comphelper::ThreadPool::ThreadWorker::execute().

◆ isIdle()

bool comphelper::ThreadPool::isIdle ( ) const
inline

return true if there are no queued or worked-on tasks

Definition at line 76 of file threadpool.hxx.

Referenced by joinThreadsIfIdle().

◆ isTaskTagDone()

bool comphelper::ThreadPool::isTaskTagDone ( const std::shared_ptr< ThreadTaskTag > &  pTag)
static

Definition at line 306 of file threadpool.cxx.

◆ joinThreadsIfIdle()

void comphelper::ThreadPool::joinThreadsIfIdle ( )

join all threads if there are no tasks presently.

Definition at line 292 of file threadpool.cxx.

References isIdle(), maMutex, and shutdownLocked().

Referenced by waitUntilDone().

◆ operator=()

ThreadPool & comphelper::ThreadPool::operator= ( const ThreadPool )
privatedelete

◆ popWorkLocked()

std::unique_ptr< ThreadTask > comphelper::ThreadPool::popWorkLocked ( std::unique_lock< std::mutex > &  rGuard,
bool  bWait 
)
private

Pop a work task.

Parameters
bWait- if set wait until task present or termination
Returns
a new task to perform, or NULL if list empty or terminated

Definition at line 233 of file threadpool.cxx.

References maTasks, maTasksChanged, and mbTerminate.

Referenced by comphelper::ThreadPool::ThreadWorker::execute(), shutdownLocked(), and waitUntilDone().

◆ pushTask()

void comphelper::ThreadPool::pushTask ( std::unique_ptr< ThreadTask pTask)

push a new task onto the work queue

Definition at line 214 of file threadpool.cxx.

References maMutex, maTasks, maTasksChanged, maWorkers, mbTerminate, mnBusyWorkers, mnMaxWorkers, and ThreadWorker.

◆ shutdown()

void comphelper::ThreadPool::shutdown ( )

wait until all work is completed, then join all threads

Definition at line 157 of file threadpool.cxx.

References maMutex, and shutdownLocked().

◆ shutdownLocked()

void comphelper::ThreadPool::shutdownLocked ( std::unique_lock< std::mutex > &  aGuard)
private

Definition at line 166 of file threadpool.cxx.

References find(), maTasks, maTasksChanged, maWorkers, mbTerminate, and popWorkLocked().

Referenced by joinThreadsIfIdle(), and shutdown().

◆ waitUntilDone()

void comphelper::ThreadPool::waitUntilDone ( const std::shared_ptr< ThreadTaskTag > &  rTag,
bool  bJoin = true 
)

Wait until all queued tasks associated with the tag are completed.

Parameters
bJoin- if set call joinThreadsIfIdle() at the end

Definition at line 264 of file threadpool.cxx.

References comphelper::gbIsWorkerThread, joinThreadsIfIdle(), maMutex, maWorkers, and popWorkLocked().

Friends And Related Function Documentation

◆ ThreadWorker

friend class ThreadWorker
friend

Definition at line 89 of file threadpool.hxx.

Referenced by pushTask().

Member Data Documentation

◆ maMutex

std::mutex comphelper::ThreadPool::maMutex
private

signalled when all in-progress tasks are complete

Definition at line 101 of file threadpool.hxx.

Referenced by comphelper::ThreadPool::ThreadWorker::execute(), joinThreadsIfIdle(), pushTask(), shutdown(), and waitUntilDone().

◆ maTasks

std::vector< std::unique_ptr<ThreadTask> > comphelper::ThreadPool::maTasks
private

Definition at line 106 of file threadpool.hxx.

Referenced by popWorkLocked(), pushTask(), shutdownLocked(), and ~ThreadPool().

◆ maTasksChanged

std::condition_variable comphelper::ThreadPool::maTasksChanged
private

Definition at line 102 of file threadpool.hxx.

Referenced by popWorkLocked(), pushTask(), and shutdownLocked().

◆ maWorkers

std::vector< rtl::Reference< ThreadWorker > > comphelper::ThreadPool::maWorkers
private

Definition at line 107 of file threadpool.hxx.

Referenced by pushTask(), shutdownLocked(), and waitUntilDone().

◆ mbTerminate

bool comphelper::ThreadPool::mbTerminate
private

◆ mnBusyWorkers

std::size_t comphelper::ThreadPool::mnBusyWorkers
private

Definition at line 105 of file threadpool.hxx.

Referenced by decBusyWorker(), incBusyWorker(), pushTask(), and ~ThreadPool().

◆ mnMaxWorkers

std::size_t const comphelper::ThreadPool::mnMaxWorkers
private

Definition at line 104 of file threadpool.hxx.

Referenced by pushTask().


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