#include <ThreadPool.h>
Public Member Functions | |
ThreadPool () | |
~ThreadPool () | |
bool | StartThreads (int numThreads, int stackSize, void *(*_perThreadInit)()=0, void(*_perThreadDeinit)(void *)=0) |
void | SetThreadDataInterface (ThreadDataInterface *tdi, void *context) |
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 | WasStarted (void) |
Did we call Start? | |
bool | Pause (void) |
void | Resume (void) |
Protected Member Functions | |
template<class ThreadInputType , class ThreadOutputType > | |
friend | RAK_THREAD_DECLARATION (WorkerThread) |
Protected Attributes | |
SimpleMutex | inputQueueMutex |
SimpleMutex | outputQueueMutex |
SimpleMutex | workingThreadCountMutex |
SimpleMutex | runThreadsMutex |
void *(* | perThreadDataFactory )() |
void(* | perThreadDataDestructor )(void *) |
DataStructures::Queue < OutputType(*)(InputType, bool *, void *)> | inputFunctionQueue |
DataStructures::Queue< InputType > | inputQueue |
DataStructures::Queue< OutputType > | outputQueue |
ThreadDataInterface * | threadDataInterface |
void * | tdiContext |
bool | runThreads |
int | numThreadsRunning |
int | numThreadsWorking |
SimpleMutex | numThreadsRunningMutex |
SignaledEvent | quitAndIncomingDataEvents |
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.
Definition at line 29 of file ThreadPool.h.
ThreadPool< InputType, OutputType >::ThreadPool | ( | ) |
Definition at line 284 of file ThreadPool.h.
ThreadPool< InputType, OutputType >::~ThreadPool | ( | ) |
void ThreadPool< InputType, OutputType >::AddInput | ( | OutputType(*)(InputType, bool *returnOutput, void *perThreadData) | workerThreadCallback, | |
InputType | inputData | |||
) |
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.
[in] | workerThreadCallback | The function to call from the thread |
[in] | inputData | The parameter to pass to userCallback |
Definition at line 382 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::AddOutput | ( | OutputType | outputData | ) |
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.
[in] | outputData | The output to inject |
Definition at line 392 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::Clear | ( | void | ) |
Clears internal buffers.
Definition at line 437 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::ClearInput | ( | void | ) |
Removes all items from the input queue.
Definition at line 511 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::ClearOutput | ( | void | ) |
Removes all items from the output queue.
Definition at line 518 of file ThreadPool.h.
InputType ThreadPool< InputType, OutputType >::GetInputAtIndex | ( | unsigned | index | ) |
Get the input at a specified index.
Definition at line 475 of file ThreadPool.h.
OutputType ThreadPool< InputType, OutputType >::GetOutput | ( | void | ) |
Gets the output of a call to userCallback HasOutput must return true before you call this function. Otherwise it will assert.
Definition at line 427 of file ThreadPool.h.
OutputType ThreadPool< InputType, OutputType >::GetOutputAtIndex | ( | unsigned | index | ) |
Get the output at a specified index.
Definition at line 501 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::HasInput | ( | void | ) |
Returns true if input from GetInput is waiting.
Definition at line 418 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::HasInputFast | ( | void | ) |
Inaccurate but fast version of HasInput. If this returns true, you should still check HasInput for the real value.
Definition at line 413 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::HasOutput | ( | void | ) |
Returns true if output from GetOutput is waiting.
Definition at line 404 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::HasOutputFast | ( | void | ) |
Inaccurate but fast version of HasOutput. If this returns true, you should still check HasOutput for the real value.
Definition at line 399 of file ThreadPool.h.
unsigned ThreadPool< InputType, OutputType >::InputSize | ( | void | ) |
Length of the input queue.
Definition at line 470 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::IsWorking | ( | void | ) |
Are any of the threads working, or is input or output available?
Definition at line 523 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::LockInput | ( | void | ) |
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
Definition at line 460 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::LockOutput | ( | void | ) |
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
Definition at line 486 of file ThreadPool.h.
int ThreadPool< InputType, OutputType >::NumThreadsWorking | ( | void | ) |
The number of currently active threads.
Definition at line 551 of file ThreadPool.h.
unsigned ThreadPool< InputType, OutputType >::OutputSize | ( | void | ) |
Length of the output queue.
Definition at line 496 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::Pause | ( | void | ) |
ThreadPool< InputType, OutputType >::RAK_THREAD_DECLARATION | ( | WorkerThread | ) | [protected] |
void ThreadPool< InputType, OutputType >::RemoveInputAtIndex | ( | unsigned | index | ) |
Remove input from a specific index. This does NOT do memory deallocation - it only removes the item from the queue.
Definition at line 480 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::RemoveOutputAtIndex | ( | unsigned | index | ) |
Remove output from a specific index. This does NOT do memory deallocation - it only removes the item from the queue.
Definition at line 506 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::Resume | ( | void | ) |
void ThreadPool< InputType, OutputType >::SetThreadDataInterface | ( | ThreadDataInterface * | tdi, | |
void * | context | |||
) |
Definition at line 348 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::StartThreads | ( | int | numThreads, | |
int | stackSize, | |||
void *(*)() | _perThreadInit = 0 , |
|||
void(*)(void *) | _perThreadDeinit = 0 | |||
) |
Start the specified number of threads.
[in] | numThreads | The number of threads to start |
[in] | stackSize | 0 for default (except on consoles). |
[in] | _perThreadInit | User callback to return data stored per thread. Pass 0 if not needed. |
[in] | _perThreadDeinit | User callback to destroy data stored per thread, created by _perThreadInit. Pass 0 if not needed. |
Definition at line 300 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::StopThreads | ( | void | ) |
Stops all threads.
Definition at line 354 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::UnlockInput | ( | void | ) |
Unlock the input buffer after you are done with the functions InputSize, GetInputAtIndex, and RemoveInputAtIndex.
Definition at line 465 of file ThreadPool.h.
void ThreadPool< InputType, OutputType >::UnlockOutput | ( | void | ) |
Unlock the output buffer after you are done with the functions OutputSize, GetOutputAtIndex, and RemoveOutputAtIndex.
Definition at line 491 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::WasStarted | ( | void | ) |
Did we call Start?
Definition at line 557 of file ThreadPool.h.
DataStructures::Queue<OutputType (*)(InputType, bool *, void*)> ThreadPool< InputType, OutputType >::inputFunctionQueue [protected] |
Definition at line 150 of file ThreadPool.h.
DataStructures::Queue<InputType> ThreadPool< InputType, OutputType >::inputQueue [protected] |
Definition at line 151 of file ThreadPool.h.
SimpleMutex ThreadPool< InputType, OutputType >::inputQueueMutex [protected] |
Definition at line 143 of file ThreadPool.h.
int ThreadPool< InputType, OutputType >::numThreadsRunning [protected] |
Definition at line 172 of file ThreadPool.h.
SimpleMutex ThreadPool< InputType, OutputType >::numThreadsRunningMutex [protected] |
Definition at line 176 of file ThreadPool.h.
int ThreadPool< InputType, OutputType >::numThreadsWorking [protected] |
Definition at line 174 of file ThreadPool.h.
DataStructures::Queue<OutputType> ThreadPool< InputType, OutputType >::outputQueue [protected] |
Definition at line 152 of file ThreadPool.h.
SimpleMutex ThreadPool< InputType, OutputType >::outputQueueMutex [protected] |
Definition at line 143 of file ThreadPool.h.
void(* ThreadPool< InputType, OutputType >::perThreadDataDestructor)(void *) [protected] |
Definition at line 146 of file ThreadPool.h.
void*(* ThreadPool< InputType, OutputType >::perThreadDataFactory)() [protected] |
Definition at line 145 of file ThreadPool.h.
SignaledEvent ThreadPool< InputType, OutputType >::quitAndIncomingDataEvents [protected] |
Definition at line 178 of file ThreadPool.h.
bool ThreadPool< InputType, OutputType >::runThreads [protected] |
Definition at line 170 of file ThreadPool.h.
SimpleMutex ThreadPool< InputType, OutputType >::runThreadsMutex [protected] |
Definition at line 143 of file ThreadPool.h.
void* ThreadPool< InputType, OutputType >::tdiContext [protected] |
Definition at line 155 of file ThreadPool.h.
ThreadDataInterface* ThreadPool< InputType, OutputType >::threadDataInterface [protected] |
Definition at line 154 of file ThreadPool.h.
SimpleMutex ThreadPool< InputType, OutputType >::workingThreadCountMutex [protected] |
Definition at line 143 of file ThreadPool.h.