#include <MultiParallel.hpp>
Protected Member Functions | |
void | activateThreads () |
void | constructThreads (int numThreads) |
void | destroyThreads () |
int | numThreads () |
void | waitForThreads () |
![]() | |
Parallel () | |
Private Member Functions | |
virtual bool | doSomeWork ()=0 |
void | reportException (FatalError *exception) |
void | run (int threadIndex) |
bool | threadsActive () |
Private Attributes | |
std::vector< bool > | _active |
std::condition_variable | _conditionChildren |
std::condition_variable | _conditionParent |
FatalError * | _exception |
std::mutex | _mutex |
int | _numThreads |
std::atomic< bool > | _terminate |
std::vector< std::thread > | _threads |
Additional Inherited Members | |
![]() | |
virtual | ~Parallel () |
virtual void | call (size_t maxIndex, std::function< void(size_t firstIndex, size_t numIndices)> target)=0 |
MultiParallel is an intermediate, abstract class that offers facilities for implementing parallel subclasses that use multiple parallel execution threads.
Specifically, the class offers functions to create a given number of child threads and hand out work to them. The parent thread is never used to perform actual work, so that it remains available for communicating with other processes. Because the parent thread is supposed to consume very little resources, it is not counted towards the number of threads configured by the user.
When an exception is thrown by one of the child threads, all other threads are gracefully shut down and a FatalError exception is thrown in the context of the parent thread. If the original exception was a FatalError instance, the newly thrown exception is a copy thereof. Otherwise a fresh FatalError instance is created with a generic error message.
This class uses the standard low-level C++ multi-threading capabilities. It is designed to minimize the run-time overhead for handing out parallel tasks. Between invocations of the call() function, the parallel threads are put in wait so that they consume no CPU cycles (and very little memory).
|
protected |
This function activates the child threads so they start doing work (i.e. calling the doSomeWork() function until it returns false).
|
protected |
This function constructs the specified number of parallel child threads (not including the parent thread) and waits for them to become ready (in the inactive state).
|
protected |
This function destructs the child threads constucted with the constructThreads() function.
|
privatepure virtual |
The function to do the actual work; called from within run(). The function should perform some limited amount of work and then return true if more work might be available for this cycle, and false if not.
Implemented in MultiHybridParallel, and MultiThreadParallel.
|
inlineprotected |
This function returns the number of parallel child threads (not including the parent thread) specified to constructThreads().
|
private |
This function reports an exception from within any thread.
|
private |
This function gets executed inside each of the parallel threads.
|
private |
This function returns true if at least one of the child threads is still active, and false if not. This function does not perform any locking; callers should lock the shared data members of this class instance.
|
protected |
This function blocks until all child threads have become inactive (i.e. the doSomeWork() function has returned false for all threads).