Open CASCADE Technology 7.8.2.dev
OSD_ThreadPool Class Reference

Class defining a thread pool for executing algorithms in multi-threaded mode. Thread pool allocates requested amount of threads and keep them alive (in sleep mode when unused) during thread pool lifetime. The same pool can be used by multiple consumers, including nested multi-threading algorithms and concurrent threads: More...

#include <OSD_ThreadPool.hxx>

Inheritance diagram for OSD_ThreadPool:

Data Structures

class  EnumeratedThread
 Thread with back reference to thread pool and thread index in it. More...
 
class  Job
 Auxiliary wrapper class for thread function. More...
 
class  JobInterface
 Thread function interface. More...
 
class  JobRange
 Auxiliary class which ensures exclusive access to iterators of processed data pool. More...
 
class  Launcher
 Launcher object locking a subset of threads (or all threads) in a thread pool to perform parallel execution of the job. More...
 

Public Member Functions

 OSD_ThreadPool (int theNbThreads=-1)
 Main constructor. Application may consider specifying more threads than actually available (OSD_Parallel::NbLogicalProcessors()) and set up NbDefaultThreadsToLaunch() to a smaller value so that concurrent threads will be able using single Thread Pool instance more efficiently.
 
virtual ~OSD_ThreadPool ()
 Destructor.
 
bool HasThreads () const
 Return TRUE if at least 2 threads are available (including self-thread).
 
int LowerThreadIndex () const
 Return the lower thread index.
 
int UpperThreadIndex () const
 Return the upper thread index (last index is reserved for self-thread).
 
int NbThreads () const
 Return the number of threads; >= 1.
 
int NbDefaultThreadsToLaunch () const
 Return maximum number of threads to be locked by a single Launcher object by default; the entire thread pool size is returned by default.
 
void SetNbDefaultThreadsToLaunch (int theNbThreads)
 Set maximum number of threads to be locked by a single Launcher object by default. Should be set BEFORE first usage.
 
bool IsInUse ()
 Checks if thread pools has active consumers.
 
void Init (int theNbThreads)
 Reinitialize the thread pool with a different number of threads. Should be called only with no active jobs, or exception Standard_ProgramError will be thrown!
 
- Public Member Functions inherited from Standard_Transient
 Standard_Transient ()
 Empty constructor.
 
 Standard_Transient (const Standard_Transient &)
 Copy constructor – does nothing.
 
Standard_Transientoperator= (const Standard_Transient &)
 Assignment operator, needed to avoid copying reference counter.
 
virtual ~Standard_Transient ()
 Destructor must be virtual.
 
virtual const opencascade::handle< Standard_Type > & DynamicType () const
 Returns a type descriptor about this object.
 
Standard_Boolean IsInstance (const opencascade::handle< Standard_Type > &theType) const
 Returns a true value if this is an instance of Type.
 
Standard_Boolean IsInstance (const Standard_CString theTypeName) const
 Returns a true value if this is an instance of TypeName.
 
Standard_Boolean IsKind (const opencascade::handle< Standard_Type > &theType) const
 Returns true if this is an instance of Type or an instance of any class that inherits from Type. Note that multiple inheritance is not supported by OCCT RTTI mechanism.
 
Standard_Boolean IsKind (const Standard_CString theTypeName) const
 Returns true if this is an instance of TypeName or an instance of any class that inherits from TypeName. Note that multiple inheritance is not supported by OCCT RTTI mechanism.
 
Standard_TransientThis () const
 Returns non-const pointer to this object (like const_cast). For protection against creating handle to objects allocated in stack or call from constructor, it will raise exception Standard_ProgramError if reference counter is zero.
 
Standard_Integer GetRefCount () const noexcept
 Get the reference counter of this object.
 
void IncrementRefCounter () noexcept
 Increments the reference counter of this object.
 
Standard_Integer DecrementRefCounter () noexcept
 Decrements the reference counter of this object; returns the decremented value.
 
virtual void Delete () const
 Memory deallocator for transient classes.
 

Static Public Member Functions

static const Handle< OSD_ThreadPool > & DefaultPool (int theNbThreads=-1)
 Return (or create) a default thread pool. Number of threads argument will be considered only when called first time.
 
- Static Public Member Functions inherited from Standard_Transient
static constexpr const char * get_type_name ()
 Returns a type descriptor about this object.
 
static const opencascade::handle< Standard_Type > & get_type_descriptor ()
 Returns type descriptor of Standard_Transient class.
 

Protected Member Functions

void release ()
 Release threads.
 

Static Protected Member Functions

static void performJob (Handle< Standard_Failure > &theFailure, OSD_ThreadPool::JobInterface *theJob, int theThreadIndex)
 Perform the job and catch exceptions.
 

Additional Inherited Members

- Public Types inherited from Standard_Transient
typedef void base_type
 Returns a type descriptor about this object.
 

Detailed Description

Class defining a thread pool for executing algorithms in multi-threaded mode. Thread pool allocates requested amount of threads and keep them alive (in sleep mode when unused) during thread pool lifetime. The same pool can be used by multiple consumers, including nested multi-threading algorithms and concurrent threads:

  • Thread pool can be used either by multi-threaded algorithm by creating OSD_ThreadPool::Launcher. The functor performing a job takes two parameters - Thread Index and Data Index: void operator(int theThreadIndex, int theDataIndex){} Multi-threaded algorithm may rely on Thread Index for allocating thread-local variables in array form, since the Thread Index is guaranteed to be within range OSD_ThreadPool::Lower() and OSD_ThreadPool::Upper().
  • Default thread pool (OSD_ThreadPool::DefaultPool()) can be used in general case, but application may prefer creating a dedicated pool for better control.
  • Default thread pool allocates the amount of threads considering concurrency level of the system (amount of logical processors). This can be overridden during OSD_ThreadPool construction or by calling OSD_ThreadPool::Init() (the pool should not be used!).
  • OSD_ThreadPool::Launcher reserves specific amount of threads from the pool for executing multi-threaded Job. Normally, single Launcher instance will occupy all threads available in thread pool, so that nested multi-threaded algorithms (within the same thread) and concurrent threads trying to use the same thread pool will run sequentially. This behavior is affected by OSD_ThreadPool::NbDefaultThreadsToLaunch() parameter and Launcher constructor, so that single Launcher instance will occupy not all threads in the pool allowing other threads to be used concurrently.
  • OSD_ThreadPool::Launcher locks thread one-by-one from thread pool in a thread-safe way.
  • Each working thread catches exceptions occurred during job execution, and Launcher will throw Standard_Failure in a caller thread on completed execution.

Constructor & Destructor Documentation

◆ OSD_ThreadPool()

OSD_ThreadPool::OSD_ThreadPool ( int theNbThreads = -1)

Main constructor. Application may consider specifying more threads than actually available (OSD_Parallel::NbLogicalProcessors()) and set up NbDefaultThreadsToLaunch() to a smaller value so that concurrent threads will be able using single Thread Pool instance more efficiently.

Parameters
theNbThreadsthreads number to be created by pool (if -1 is specified then OSD_Parallel::NbLogicalProcessors() will be used)

◆ ~OSD_ThreadPool()

virtual OSD_ThreadPool::~OSD_ThreadPool ( )
virtual

Destructor.

Member Function Documentation

◆ DefaultPool()

static const Handle< OSD_ThreadPool > & OSD_ThreadPool::DefaultPool ( int theNbThreads = -1)
static

Return (or create) a default thread pool. Number of threads argument will be considered only when called first time.

◆ HasThreads()

bool OSD_ThreadPool::HasThreads ( ) const
inline

Return TRUE if at least 2 threads are available (including self-thread).

◆ Init()

void OSD_ThreadPool::Init ( int theNbThreads)

Reinitialize the thread pool with a different number of threads. Should be called only with no active jobs, or exception Standard_ProgramError will be thrown!

◆ IsInUse()

bool OSD_ThreadPool::IsInUse ( )

Checks if thread pools has active consumers.

◆ LowerThreadIndex()

int OSD_ThreadPool::LowerThreadIndex ( ) const
inline

Return the lower thread index.

◆ NbDefaultThreadsToLaunch()

int OSD_ThreadPool::NbDefaultThreadsToLaunch ( ) const
inline

Return maximum number of threads to be locked by a single Launcher object by default; the entire thread pool size is returned by default.

◆ NbThreads()

int OSD_ThreadPool::NbThreads ( ) const
inline

Return the number of threads; >= 1.

◆ performJob()

static void OSD_ThreadPool::performJob ( Handle< Standard_Failure > & theFailure,
OSD_ThreadPool::JobInterface * theJob,
int theThreadIndex )
staticprotected

Perform the job and catch exceptions.

◆ release()

void OSD_ThreadPool::release ( )
protected

Release threads.

◆ SetNbDefaultThreadsToLaunch()

void OSD_ThreadPool::SetNbDefaultThreadsToLaunch ( int theNbThreads)
inline

Set maximum number of threads to be locked by a single Launcher object by default. Should be set BEFORE first usage.

◆ UpperThreadIndex()

int OSD_ThreadPool::UpperThreadIndex ( ) const
inline

Return the upper thread index (last index is reserved for self-thread).


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