Simbody
|
This class is used for performing multithreaded computations. More...
#include <ParallelExecutor.h>
Classes | |
class | Task |
Concrete subclasses of this abstract class represent tasks that can be executed by a ParallelExecutor. More... | |
Public Member Functions | |
ParallelExecutor (int numThreads=getNumProcessors()) | |
Construct a ParallelExecutor. | |
void | execute (Task &task, int times) |
Execute a parallel task. | |
Static Public Member Functions | |
static int | getNumProcessors () |
Get the number of available processor cores. | |
static bool | isWorkerThread () |
Determine whether the thread invoking this method is a worker thread created by ParallelExecutor. |
This class is used for performing multithreaded computations.
To use it, define a subclass of ParallelExecutor::Task that performs some computation. Then create a ParallelExecutor object and ask it to execute the task:
ParallelExecutor executor; executor.execute(myTask, times);
The Task's execute() method will be called the specified number of times, with each invocation being given a different index value from 0 to times-1. The invocations are done in parallel on multiple threads, so you cannot make any assumptions about what order they will occur in or which ones will happen at the same time.
The threads are created in the ParallelExecutor's constructor and remain active until it is deleted. This means that creating a ParallelExecutor is a somewhat expensive operation, but it may then be used repeatedly for executing various calculations. By default, the number of threads is chosen to be equal to the number of available processor cores. You can optionally specify a different number of threads to create. For example, using more threads than processors can sometimes lead to better processor utilitization. Alternatively, if the Task will only be executed four times, you might specify min(4, ParallelExecutor::getNumProcessors()) to avoid creating extra threads that will never have any work to do.
SimTK::ParallelExecutor::ParallelExecutor | ( | int | numThreads = getNumProcessors() | ) | [explicit] |
Construct a ParallelExecutor.
numThreads | the number of threads to create. By default, this is set equal to the number of processors. |
void SimTK::ParallelExecutor::execute | ( | Task & | task, |
int | times | ||
) |
static int SimTK::ParallelExecutor::getNumProcessors | ( | ) | [static] |
Get the number of available processor cores.
static bool SimTK::ParallelExecutor::isWorkerThread | ( | ) | [static] |
Determine whether the thread invoking this method is a worker thread created by ParallelExecutor.