OpenMM
|
A ThreadPool creates a set of worker threads that can be used to execute tasks in parallel. More...
#include <ThreadPool.h>
Public Member Functions | |
ThreadPool (int numThreads=0) | |
Create a ThreadPool. More... | |
~ThreadPool () | |
int | getNumThreads () const |
Get the number of worker threads in the pool. More... | |
void | execute (Task &task) |
Execute a Task in parallel on the worker threads. More... | |
void | syncThreads () |
This is called by the worker threads to block until all threads have reached the same point and the master thread instructs them to continue by calling resumeThreads(). More... | |
void | waitForThreads () |
This is called by the master thread to wait until all threads have completed the Task. More... | |
void | resumeThreads () |
Instruct the threads to resume running after blocking at a synchronization point. More... | |
virtual void | execute (ThreadPool &pool, int threadIndex)=0 |
Execute the task on each thread. More... | |
A ThreadPool creates a set of worker threads that can be used to execute tasks in parallel.
This defines a task that can be executed in parallel by the worker threads.
After creating a ThreadPool, call execute() to start a task running then waitForThreads() to block until all threads have finished. You also can synchronize the threads in the middle of the task by having them call syncThreads(). In this case, the parent thread should call waitForThreads() an additional time; each call waits until all worker threads have reached the next syncThreads(), and the final call waits until they exit from the Task's execute() method. After calling waitForThreads() to block at a synchronization point, the parent thread should call resumeThreads() to instruct the worker threads to resume.
ThreadPool | ( | int | numThreads = 0 | ) |
Create a ThreadPool.
numThreads | the number of worker threads to create. If this is 0 (the default), the number of threads is set equal to the number of logical CPU cores available |
~ThreadPool | ( | ) |
void execute | ( | Task & | task | ) |
Execute a Task in parallel on the worker threads.
|
pure virtual |
Execute the task on each thread.
pool | the ThreadPool being used to execute the task |
threadIndex | the index of the thread invoking this method |
int getNumThreads | ( | ) | const |
Get the number of worker threads in the pool.
void resumeThreads | ( | ) |
Instruct the threads to resume running after blocking at a synchronization point.
void syncThreads | ( | ) |
This is called by the worker threads to block until all threads have reached the same point and the master thread instructs them to continue by calling resumeThreads().
void waitForThreads | ( | ) |
This is called by the master thread to wait until all threads have completed the Task.
Alternatively, if the threads call syncThreads(), this blocks until all threads have reached the synchronization point.