Simbody  3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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...

+ Inheritance diagram for SimTK::ParallelWorkQueue:

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. More...
 
void addTask (Task *task)
 Add a Task to the queue. More...
 
void flush ()
 Block until all Tasks that have been added to the queue finish executing. More...
 
- Public Member Functions inherited from SimTK::PIMPLHandle< ParallelWorkQueue, ParallelWorkQueueImpl >
bool isEmptyHandle () const
 Returns true if this handle is empty, that is, does not refer to any implementation object. More...
 
bool isOwnerHandle () const
 Returns true if this handle is the owner of the implementation object to which it refers. More...
 
bool isSameHandle (const ParallelWorkQueue &other) const
 Determine whether the supplied handle is the same object as "this" PIMPLHandle. More...
 
void disown (ParallelWorkQueue &newOwner)
 Give up ownership of the implementation to an empty handle. More...
 
PIMPLHandlereferenceAssign (const ParallelWorkQueue &source)
 "Copy" assignment but with shallow (pointer) semantics. More...
 
PIMPLHandlecopyAssign (const ParallelWorkQueue &source)
 This is real copy assignment, with ordinary C++ object ("value") semantics. More...
 
void clearHandle ()
 Make this an empty handle, deleting the implementation object if this handle is the owner of it. More...
 
const ParallelWorkQueueImpl & getImpl () const
 Get a const reference to the implementation associated with this Handle. More...
 
ParallelWorkQueueImpl & updImpl ()
 Get a writable reference to the implementation associated with this Handle. More...
 
int getImplHandleCount () const
 Return the number of handles the implementation believes are referencing it. More...
 

Additional Inherited Members

- Public Types inherited from SimTK::PIMPLHandle< ParallelWorkQueue, ParallelWorkQueueImpl >
typedef PIMPLHandle
< ParallelWorkQueue,
ParallelWorkQueueImpl, PTR > 
HandleBase
 
typedef HandleBase ParentHandle
 
- Protected Member Functions inherited from SimTK::PIMPLHandle< ParallelWorkQueue, ParallelWorkQueueImpl >
 PIMPLHandle ()
 The default constructor makes this an empty handle. More...
 
 PIMPLHandle (ParallelWorkQueueImpl *p)
 This provides consruction of a handle referencing an existing implementation object. More...
 
 PIMPLHandle (const PIMPLHandle &source)
 The copy constructor makes either a deep (value) or shallow (reference) copy of the supplied source PIMPL object, based on whether this is a "pointer semantics" (PTR=true) or "object (value) semantics" (PTR=false, default) class. More...
 
 ~PIMPLHandle ()
 Note that the destructor is non-virtual. More...
 
PIMPLHandleoperator= (const PIMPLHandle &source)
 Copy assignment makes the current handle either a deep (value) or shallow (reference) copy of the supplied source PIMPL object, based on whether this is a "pointer sematics" (PTR=true) or "object (value) semantics" (PTR=false, default) class. More...
 
void setImpl (ParallelWorkQueueImpl *p)
 Set the implementation for this empty handle. More...
 
bool hasSameImplementation (const ParallelWorkQueue &other) const
 Determine whether the supplied handle is a reference to the same implementation object as is referenced by "this" PIMPLHandle. More...
 

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: