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 bJoinAll=true)
 Wait until all queued tasks associated with the tag are completed. More...
 
void joinAll ()
 join all threads if there are no tasks presently. 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 > &)
 

Private Attributes

std::mutex maMutex
 signalled when all in-progress tasks are complete More...
 
std::condition_variable maTasksChanged
 
bool mbTerminate
 
std::size_t const mnWorkers
 
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 92 of file threadpool.cxx.

comphelper::ThreadPool::~ThreadPool ( )

Definition at line 98 of file threadpool.cxx.

References maTasks, and mbTerminate.

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

Member Function Documentation

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

Definition at line 260 of file threadpool.cxx.

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

Referenced by comphelper::ThreadPoolStatic::operator()().

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

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

Definition at line 117 of file threadpool.cxx.

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

return the number of live worker threads

Definition at line 75 of file threadpool.hxx.

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

Definition at line 265 of file threadpool.cxx.

void comphelper::ThreadPool::joinAll ( )

join all threads if there are no tasks presently.

Definition at line 251 of file threadpool.cxx.

References maMutex, maTasks, 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 208 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 190 of file threadpool.cxx.

References maMutex, maTasks, maTasksChanged, maWorkers, mbTerminate, mnWorkers, and ThreadWorker.

void comphelper::ThreadPool::shutdown ( )

wait until all work is completed, then join all threads

Definition at line 144 of file threadpool.cxx.

References maMutex, and shutdownLocked().

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

Definition at line 153 of file threadpool.cxx.

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

Referenced by joinAll(), and shutdown().

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

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

Parameters
bJoinAll- if set it joins all threads at the end if no other tasks from other tags.

Definition at line 228 of file threadpool.cxx.

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

Friends And Related Function Documentation

friend class ThreadWorker
friend

Definition at line 84 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 95 of file threadpool.hxx.

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

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

Definition at line 99 of file threadpool.hxx.

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

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

Definition at line 96 of file threadpool.hxx.

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

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

Definition at line 100 of file threadpool.hxx.

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

bool comphelper::ThreadPool::mbTerminate
private
std::size_t const comphelper::ThreadPool::mnWorkers
private

Definition at line 98 of file threadpool.hxx.

Referenced by pushTask().


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