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 (sal_Int32 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 sal_Int32 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 43 of file threadpool.hxx.

Constructor & Destructor Documentation

comphelper::ThreadPool::ThreadPool ( sal_Int32  nWorkers)

Definition at line 93 of file threadpool.cxx.

comphelper::ThreadPool::~ThreadPool ( )

Definition at line 100 of file threadpool.cxx.

References maTasks, mbTerminate, and mnBusyWorkers.

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

Member Function Documentation

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

Definition at line 297 of file threadpool.cxx.

void comphelper::ThreadPool::decBusyWorker ( )
private

Definition at line 254 of file threadpool.cxx.

References mnBusyWorkers.

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

sal_Int32 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 131 of file threadpool.cxx.

References U.

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

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

Definition at line 126 of file threadpool.cxx.

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

return the number of live worker threads

Definition at line 78 of file threadpool.hxx.

void comphelper::ThreadPool::incBusyWorker ( )
private

Definition at line 249 of file threadpool.cxx.

References mnBusyWorkers.

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

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

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

Definition at line 75 of file threadpool.hxx.

Referenced by joinThreadsIfIdle().

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

Definition at line 302 of file threadpool.cxx.

void comphelper::ThreadPool::joinThreadsIfIdle ( )

join all threads if there are no tasks presently.

Definition at line 288 of file threadpool.cxx.

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

Referenced by waitUntilDone().

ThreadPool& comphelper::ThreadPool::operator= ( const ThreadPool )
privatedelete
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 229 of file threadpool.cxx.

References maTasks, maTasksChanged, and mbTerminate.

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

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

push a new task onto the work queue

Definition at line 210 of file threadpool.cxx.

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

void comphelper::ThreadPool::shutdown ( )

wait until all work is completed, then join all threads

Definition at line 153 of file threadpool.cxx.

References maMutex, and shutdownLocked().

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

Definition at line 162 of file threadpool.cxx.

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

Referenced by joinThreadsIfIdle(), and shutdown().

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 260 of file threadpool.cxx.

References joinThreadsIfIdle(), maMutex, maWorkers, and popWorkLocked().

Friends And Related Function Documentation

friend class ThreadWorker
friend

Definition at line 87 of file threadpool.hxx.

Referenced by pushTask().

Member Data Documentation

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

signalled when all in-progress tasks are complete

Definition at line 100 of file threadpool.hxx.

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

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

Definition at line 105 of file threadpool.hxx.

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

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

Definition at line 101 of file threadpool.hxx.

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

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

Definition at line 106 of file threadpool.hxx.

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

bool comphelper::ThreadPool::mbTerminate
private
std::size_t comphelper::ThreadPool::mnBusyWorkers
private

Definition at line 104 of file threadpool.hxx.

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

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

Definition at line 103 of file threadpool.hxx.

Referenced by pushTask().


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