ThreadPool< InputType, OutputType > Struct Template Reference

#include <ThreadPool.h>

List of all members.

Public Member Functions

bool StartThreads (int numThreads, int stackSize)
void StopThreads (void)
 Stops all threads.
void AddInput (OutputType(*workerThreadCallback)(InputType, bool *returnOutput), InputType inputData)
bool HasOutput (void)
bool HasOutputFast (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.

Protected Attributes

SimpleMutex inputQueueMutex
SimpleMutex outputQueueMutex
DataStructures::Queue< OutputType(*)(InputType, bool *returnOutput)> inputFunctionQueue
DataStructures::Queue< InputType > inputDataQueue
DataStructures::Queue< OutputType > outputQueue
bool threadsRunning
int numThreadsRunning
SimpleMutex numThreadsRunningMutex

Friends

template<class InputType, class OutputType>
void * WorkerThread (void *arguments)


Detailed Description

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

A simple class to create work 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)  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 inputDataQueue 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>
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.

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 >::HasOutput void   ) 
 

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   ) 
 

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   ) 
 

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   ) 
 

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
 

Start the specified number of threads.

Parameters:
[in] numThreads The number of threads to start
[in] stackSize 0 for default (except on consoles).
Returns:
True on success, false on failure.


The documentation for this struct was generated from the following file:
Generated on Sat Oct 14 08:37:40 2006 for RakNet by  doxygen 1.4.6-NO