# ThreadPool Class Reference

A ThreadPool creates a set of worker threads that can be used to execute tasks in parallel. More...

`#include <ThreadPool.h>`

List of all members.

## Public Member Functions
| ThreadPool (int numThreads=0)
| Create a ThreadPool.
| ~ThreadPool ()
int | getNumThreads () const
| Get the number of worker threads in the pool.
void | execute (Task &task)
| Execute a Task in parallel on the worker threads.
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.
void | resumeThreads ()
| Instruct the threads to resume running after blocking at a synchronization point.
virtual void | execute (ThreadPool &pool, int threadIndex)=0
| Execute the task on each thread.

## Detailed Description

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.

## Constructor & Destructor Documentation

ThreadPool | ( | int | numThreads = `0` | ) |

Create a ThreadPool.

Parameters:

  | 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 | ( |  | ) |

## Member Function Documentation

virtual void execute | ( | ThreadPool & | pool,
|  | int | threadIndex |
| ) |  |  | `[pure virtual]`

Execute the task on each thread.

Parameters:

  | pool | the ThreadPool being used to execute the task
  | threadIndex | the index of the thread invoking this method

void execute | ( | Task & | task | ) |

Execute a Task in parallel on the worker threads.

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.

The documentation for this class was generated from the following file:

- ThreadPool.h