SPH
Public Member Functions | Static Public Member Functions | Friends | List of all members
ThreadPool Class Reference

Thread pool capable of executing tasks concurrently. More...

#include <Pool.h>

Inheritance diagram for ThreadPool:
IScheduler Polymorphic

Public Member Functions

 ThreadPool (const Size numThreads=0, const Size granularity=1000)
 Initialize thread pool given the number of threads to use. More...
 
 ~ThreadPool ()
 
virtual SharedPtr< ITasksubmit (const Function< void()> &task) override
 Submits a task into the thread pool. More...
 
virtual Optional< SizegetThreadIdx () const override
 Returns the index of this thread, or NOTHING if this thread was not invoked by the thread pool. More...
 
virtual Size getThreadCnt () const override
 Returns the number of threads used by this thread pool. More...
 
virtual Size getRecommendedGranularity () const override
 Returns a value of granularity that is expected to perform well with the current thread count. More...
 
void waitForAll ()
 Blocks until all submitted tasks has been finished. More...
 
Size remainingTaskCnt ()
 Returns the number of unfinished tasks. More...
 
void setGranularity (const Size newGranularity)
 Modifies the default granularity of the thread pool. More...
 
- Public Member Functions inherited from IScheduler
virtual void parallelFor (const Size from, const Size to, const Size granularity, const Function< void(Size n1, Size n2)> &functor)
 Processes the given range concurrently. More...
 
- Public Member Functions inherited from Polymorphic
virtual ~Polymorphic ()
 

Static Public Member Functions

static SharedPtr< ThreadPoolgetGlobalInstance ()
 Returns the global instance of the thread pool. More...
 

Friends

class Task
 

Detailed Description

Thread pool capable of executing tasks concurrently.

Definition at line 70 of file Pool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

ThreadPool::ThreadPool ( const Size  numThreads = 0,
const Size  granularity = 1000 
)

Initialize thread pool given the number of threads to use.

By default, all available threads are used.

Definition at line 120 of file Pool.cpp.

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )

Definition at line 145 of file Pool.cpp.

Member Function Documentation

◆ getGlobalInstance()

SharedPtr< ThreadPool > ThreadPool::getGlobalInstance ( )
static

Returns the global instance of the thread pool.

Other instances can be constructed if needed.

Definition at line 211 of file Pool.cpp.

◆ getRecommendedGranularity()

Size ThreadPool::getRecommendedGranularity ( ) const
overridevirtual

Returns a value of granularity that is expected to perform well with the current thread count.

Implements IScheduler.

Definition at line 195 of file Pool.cpp.

◆ getThreadCnt()

Size ThreadPool::getThreadCnt ( ) const
overridevirtual

Returns the number of threads used by this thread pool.

Note that this number is constant during the lifetime of thread pool.

Implements IScheduler.

Definition at line 207 of file Pool.cpp.

◆ getThreadIdx()

Optional< Size > ThreadPool::getThreadIdx ( ) const
overridevirtual

Returns the index of this thread, or NOTHING if this thread was not invoked by the thread pool.

The index is within [0, numThreads-1].

Implements IScheduler.

Definition at line 199 of file Pool.cpp.

◆ remainingTaskCnt()

Size ThreadPool::remainingTaskCnt ( )
inline

Returns the number of unfinished tasks.

This includes both tasks currently running and tasks waiting in processing queue.

Definition at line 132 of file Pool.h.

◆ setGranularity()

void ThreadPool::setGranularity ( const Size  newGranularity)
inline

Modifies the default granularity of the thread pool.

Definition at line 137 of file Pool.h.

◆ submit()

SharedPtr< ITask > ThreadPool::submit ( const Function< void()> &  task)
overridevirtual

Submits a task into the thread pool.

The task will be executed asynchronously once tasks submitted before it are completed.

Implements IScheduler.

Definition at line 157 of file Pool.cpp.

◆ waitForAll()

void ThreadPool::waitForAll ( )

Blocks until all submitted tasks has been finished.

Definition at line 187 of file Pool.cpp.

Friends And Related Function Documentation

◆ Task

friend class Task
friend

Definition at line 71 of file Pool.h.


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