Simbody
Classes | Public Member Functions

SimTK::ParallelWorkQueue Class Reference

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>

Inheritance diagram for SimTK::ParallelWorkQueue:

List of all members.

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.

Detailed Description

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.


Constructor & Destructor Documentation

SimTK::ParallelWorkQueue::ParallelWorkQueue ( int  queueSize,
int  numThreads = ParallelExecutor::getNumProcessors() 
) [explicit]

Construct a ParallelWorkQueue.

Parameters:
queueSizethe maximum number of Tasks that can be in the queue waiting to start executing at any time
numThreadsthe number of threads to create. By default, this is set equal to the number of processors.

Member Function Documentation

void SimTK::ParallelWorkQueue::addTask ( Task task)

Add a Task to the queue.

If the queue is currently full, this method will block until space is freed up in the queue by the worker threads. The queue assumes ownership of the Task object and deletes it once it has finished executing.

Parameters:
taskthe Task to add to the queue
void SimTK::ParallelWorkQueue::flush ( )

Block until all Tasks that have been added to the queue finish executing.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines