Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
Utils::ThreadsPool< Job > Class Template Reference

ThreadsPool class for holding and controlling a pool of threads. More...

#include <ThreadsPool.h>

Public Member Functions

 ThreadsPool (int nrThreads=0)
 Construct a new Thread pool object.
 ~ThreadsPool ()
 Destructor.
void Stop ()
 Stop all the threads in the threads pool.
void Start ()
 Start all the threads in the threads pool.
void AddRunJob (const std::shared_ptr< Job > &job)
 Add a job to be executed by the threads pool.
void AddRunJob (std::shared_ptr< Job > &&job)
 Add a job to be executed by the threads pool.
void WaitForFinish ()
 Wait for all jobs to finish.
void Resize (size_t nrThreads)
 Resize the threads pool.
void SetFinishLimit (size_t limit)
 Set the finish limit.

Friends

class WorkerThread< ThreadsPool< Job >, Job >

Detailed Description

template<class Job>
class Utils::ThreadsPool< Job >

ThreadsPool class for holding and controlling a pool of threads.

Contains a bunch of threads that can execute jobs, supplying functions to add jobs, wait for their completion, start and stop the threads. Allows adding operations and converting them to prepare the circuit for distributed computing.

Template Parameters
JobThe job class/type.
See also
WorkerThread

Definition at line 38 of file ThreadsPool.h.

Constructor & Destructor Documentation

◆ ThreadsPool()

template<class Job>
Utils::ThreadsPool< Job >::ThreadsPool ( int nrThreads = 0)
inlineexplicit

Construct a new Thread pool object.

Constructs a new Thread pool object with the given number of threads.

Parameters
nrThreadsThe number of threads to create in the pool. If less than or equal to zero, one thread will be created. Can be resized later.

Definition at line 50 of file ThreadsPool.h.

◆ ~ThreadsPool()

template<class Job>
Utils::ThreadsPool< Job >::~ThreadsPool ( )
inline

Destructor.

Destroy the threads pool object. Before destroying, stops all threads and waits for their completion.

Definition at line 64 of file ThreadsPool.h.

Member Function Documentation

◆ AddRunJob() [1/2]

template<class Job>
void Utils::ThreadsPool< Job >::AddRunJob ( const std::shared_ptr< Job > & job)
inline

Add a job to be executed by the threads pool.

Adds a job to the queue of jobs to be executed by the threads pool. Notifies one thread that there is a new job to execute.

Parameters
jobThe job to be executed.

Definition at line 105 of file ThreadsPool.h.

◆ AddRunJob() [2/2]

template<class Job>
void Utils::ThreadsPool< Job >::AddRunJob ( std::shared_ptr< Job > && job)
inline

Add a job to be executed by the threads pool.

Adds a job to the queue of jobs to be executed by the threads pool. Notifies one thread that there is a new job to execute.

Parameters
jobThe job to be executed.

Definition at line 121 of file ThreadsPool.h.

◆ Resize()

template<class Job>
void Utils::ThreadsPool< Job >::Resize ( size_t nrThreads)
inline

Resize the threads pool.

Resizes the threads pool to the given number of threads. If the new size is greater than the current size, new threads will be created and started. If the new size is less than the current size, excess threads will be stopped.

Parameters
nrThreadsThe new number of threads in the pool. If less than or equal to zero, one thread will be created or remain.

Definition at line 162 of file ThreadsPool.h.

◆ SetFinishLimit()

template<class Job>
void Utils::ThreadsPool< Job >::SetFinishLimit ( size_t limit)
inline

Set the finish limit.

Sets the finish limit for the threads pool. The finish limit is the number of jobs that need to be finished before WaitForFinish() returns. If no finish limit is set, WaitForFinish() will wait indefinitely.

Parameters
limitThe finish limit. Default is std::numeric_limits<size_t>::max().

Definition at line 201 of file ThreadsPool.h.

◆ Start()

template<class Job>
void Utils::ThreadsPool< Job >::Start ( )
inline

Start all the threads in the threads pool.

Starts all the threads in the threads pool.

Definition at line 93 of file ThreadsPool.h.

◆ Stop()

template<class Job>
void Utils::ThreadsPool< Job >::Stop ( )
inline

Stop all the threads in the threads pool.

Stops all the threads and clears the threads pool. Waits for all threads to complete before returning.

Definition at line 72 of file ThreadsPool.h.

◆ WaitForFinish()

template<class Job>
void Utils::ThreadsPool< Job >::WaitForFinish ( )
inline

Wait for all jobs to finish.

Waits for all jobs to finish executing. This function will block until the number of finished jobs reaches the finish limit set by SetFinishLimit(). If no finish limit is set, it will wait indefinitely.

Definition at line 138 of file ThreadsPool.h.

◆ WorkerThread< ThreadsPool< Job >, Job >

template<class Job>
friend class WorkerThread< ThreadsPool< Job >, Job >
friend

Definition at line 1 of file ThreadsPool.h.


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