# ThreadPool< InputType, OutputType > Struct Template Reference

`#include <ThreadPool.h>`

List of all members.

## Public Member Functions
bool | StartThreads (int numThreads, int stackSize, void *(*_perThreadDataFactory)()=0, void(*_perThreadDataDestructor)(void *)=0)
void | StopThreads (void)
| Stops all threads.
void | AddInput (OutputType(*workerThreadCallback)(InputType, bool *returnOutput, void *perThreadData), InputType inputData)
void | AddOutput (OutputType outputData)
bool | HasOutput (void)
bool | HasOutputFast (void)
bool | HasInput (void)
bool | HasInputFast (void)
OutputType | GetOutput (void)
void | Clear (void)
| Clears internal buffers.
void | LockInput (void)
void | UnlockInput (void)
| Unlock the input buffer after you are done with the functions InputSize, GetInputAtIndex, and RemoveInputAtIndex.
unsigned | InputSize (void)
| Length of the input queue.
InputType | GetInputAtIndex (unsigned index)
| Get the input at a specified index.
void | RemoveInputAtIndex (unsigned index)
| Remove input from a specific index. This does NOT do memory deallocation - it only removes the item from the queue.
void | LockOutput (void)
void | UnlockOutput (void)
| Unlock the output buffer after you are done with the functions OutputSize, GetOutputAtIndex, and RemoveOutputAtIndex.
unsigned | OutputSize (void)
| Length of the output queue.
OutputType | GetOutputAtIndex (unsigned index)
| Get the output at a specified index.
void | RemoveOutputAtIndex (unsigned index)
| Remove output from a specific index. This does NOT do memory deallocation - it only removes the item from the queue.
void | ClearInput (void)
| Removes all items from the input queue.
void | ClearOutput (void)
| Removes all items from the output queue.
bool | IsWorking (void)
| Are any of the threads working, or is input or output available?
int | NumThreadsWorking (void)
| The number of currently active threads.
bool | WasStopped (void) const
| Have the threads been signaled to be stopped?

## Detailed Description

### template<class InputType, class OutputType>
struct ThreadPool< InputType, OutputType >

A simple class to create worker threads that processes a queue of functions with data. This class does not allocate or deallocate memory. It is up to the user to handle memory management. InputType and OutputType are stored directly in a queue. For large structures, if you plan to delete from the middle of the queue, you might wish to store pointers rather than the structures themselves so the array can shift efficiently.

## Member Function Documentation

template<class InputType , class OutputType >

void ThreadPool< InputType, OutputType >::AddInput | ( | OutputType(*)(InputType, bool *returnOutput, void *perThreadData) | workerThreadCallback,
|  | InputType | inputData |
| ) |  |  | `[inline]`

Adds a function to a queue with data to pass to that function. This function will be called from the thread Memory management is your responsibility! This class does not allocate or deallocate memory. The best way to deallocate inputData is in userCallback. If you call EndThreads such that callbacks were not called, you can iterate through the inputQueue and deallocate all pending input data there The best way to deallocate output is as it is returned to you from GetOutput. Similarly, if you end the threads such that not all output was returned, you can iterate through outputQueue and deallocate it there.

Parameters:

  `[in]` | workerThreadCallback | The function to call from the thread
  `[in]` | inputData | The parameter to pass to userCallback

template<class InputType , class OutputType >

void ThreadPool< InputType, OutputType >::AddOutput | ( | OutputType | outputData | ) | `[inline]`

Adds to the output queue Use it if you want to inject output into the same queue that the system uses. Normally you would not use this. Consider it a convenience function.

Parameters:

  `[in]` | outputData | The output to inject

template<class InputType , class OutputType >

OutputType ThreadPool< InputType, OutputType >::GetOutput | ( | void |  | ) | `[inline]`

Gets the output of a call to userCallback HasOutput must return true before you call this function. Otherwise it will assert.

Returns:
  The output of userCallback. If you have different output signatures, it is up to you to encode the data to indicate this

template<class InputType , class OutputType >

bool ThreadPool< InputType, OutputType >::HasInput | ( | void |  | ) | `[inline]`

Returns true if input from GetInput is waiting.

Returns:
  true if input is waiting, false otherwise

template<class InputType , class OutputType >

bool ThreadPool< InputType, OutputType >::HasInputFast | ( | void |  | ) | `[inline]`

Inaccurate but fast version of HasInput. If this returns true, you should still check HasInput for the real value.

Returns:
  true if input is probably waiting, false otherwise

template<class InputType , class OutputType >

bool ThreadPool< InputType, OutputType >::HasOutput | ( | void |  | ) | `[inline]`

Returns true if output from GetOutput is waiting.

Returns:
  true if output is waiting, false otherwise

template<class InputType , class OutputType >

bool ThreadPool< InputType, OutputType >::HasOutputFast | ( | void |  | ) | `[inline]`

Inaccurate but fast version of HasOutput. If this returns true, you should still check HasOutput for the real value.

Returns:
  true if output is probably waiting, false otherwise

template<class InputType , class OutputType >

void ThreadPool< InputType, OutputType >::LockInput | ( | void |  | ) | `[inline]`

Lock the input buffer before calling the functions InputSize, InputAtIndex, and RemoveInputAtIndex It is only necessary to lock the input or output while the threads are running

template<class InputType , class OutputType >

void ThreadPool< InputType, OutputType >::LockOutput | ( | void |  | ) | `[inline]`

Lock the output buffer before calling the functions OutputSize, OutputAtIndex, and RemoveOutputAtIndex It is only necessary to lock the input or output while the threads are running

template<class InputType , class OutputType >

bool ThreadPool< InputType, OutputType >::StartThreads | ( | int | numThreads,
|  | int | stackSize,
|  | void *(*)() | _perThreadDataFactory = `0`,
|  | void(*)(void *) | _perThreadDataDestructor = `0` |
| ) |  |  | `[inline]`

Start the specified number of threads.

Parameters:

  `[in]` | numThreads | The number of threads to start
  `[in]` | stackSize | 0 for default (except on consoles).
  `[in]` | _perThreadDataFactory | User callback to return data stored per thread. Pass 0 if not needed.
  `[in]` | _perThreadDataDestructor | User callback to destroy data stored per thread, created by _perThreadDataFactory. Pass 0 if not needed.

Returns:
  True on success, false on failure.

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

- C:/RakNet/Source/ThreadPool.h