Simbody
|
This class is used for performing multithreaded computations. It maintains a queue of tasks to be executed, and a pool of threads for executing them. More...
#include <ParallelWorkQueue.h>
Classes | |
class | Task |
Concrete subclasses of this abstract class represent tasks that can be executed by a ParallelWorkQueue. More... | |
Public Member Functions | |
ParallelWorkQueue (int queueSize, int numThreads=ParallelExecutor::getNumProcessors()) | |
Construct a ParallelWorkQueue. | |
void | addTask (Task *task) |
Add a Task to the queue. | |
void | flush () |
Block until all Tasks that have been added to the queue finish executing. |
This class is used for performing multithreaded computations. It maintains a queue of tasks to be executed, and a pool of threads for executing them.
To use it, define one or more subclasses of ParallelWorkQueue::Task that performs computations. Then create a ParallelWorkQueue and add Tasks to it:
ParallelWorkQueue queue(20); queue.addTask(new MyTask());
Each Task's execute() method will be called, and then the Task will be deleted. 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 ParallelWorkQueue's constructor and remain active until it is deleted. This means that creating a ParallelWorkQueue 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 only four Tasks will be executed, you might specify min(4, ParallelExecutor::getNumProcessors()) to avoid creating extra threads that will never have any work to do.
SimTK::ParallelWorkQueue::ParallelWorkQueue | ( | int | queueSize, |
int | numThreads = ParallelExecutor::getNumProcessors() |
||
) | [explicit] |
Construct a ParallelWorkQueue.
queueSize | the maximum number of Tasks that can be in the queue waiting to start executing at any time |
numThreads | the number of threads to create. By default, this is set equal to the number of processors. |
void SimTK::ParallelWorkQueue::addTask | ( | Task * | task | ) |
void SimTK::ParallelWorkQueue::flush | ( | ) |
Block until all Tasks that have been added to the queue finish executing.