SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThreadPool< InputType, OutputType > Struct Template Reference

#include <slikenet/ThreadPool.h>

+ Inheritance diagram for ThreadPool< InputType, OutputType >:

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

SLNet::SimpleMutex inputQueueMutex
 
SLNet::SimpleMutex outputQueueMutex
 
SLNet::SimpleMutex workingThreadCountMutex
 
SLNet::SimpleMutex runThreadsMutex
 
void *(* perThreadDataFactory )()
 
void(* perThreadDataDestructor )(void *)
 
DataStructures::Queue
< OutputType(*)(InputType,
bool *, void *)> 
inputFunctionQueue
 
DataStructures::Queue< InputType > inputQueue
 
DataStructures::Queue< OutputType > outputQueue
 
ThreadDataInterfacethreadDataInterface
 
void * tdiContext
 
bool runThreads
 
int numThreadsRunning
 
int numThreadsWorking
 
SLNet::SimpleMutex numThreadsRunningMutex
 
SLNet::SignaledEvent quitAndIncomingDataEvents
 

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.

Definition at line 40 of file ThreadPool.h.

Constructor & Destructor Documentation

template<class InputType , class OutputType >
ThreadPool< InputType, OutputType >::ThreadPool ( )

Definition at line 307 of file ThreadPool.h.

References numThreadsRunning.

template<class InputType , class OutputType >
ThreadPool< InputType, OutputType >::~ThreadPool ( )

Definition at line 317 of file ThreadPool.h.

Member Function Documentation

template<class InputType, class OutputType>
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.

Parameters
[in]workerThreadCallbackThe function to call from the thread
[in]inputDataThe parameter to pass to userCallback

Definition at line 422 of file ThreadPool.h.

References _FILE_AND_LINE_.

template<class InputType , class OutputType>
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.

Parameters
[in]outputDataThe output to inject

Definition at line 432 of file ThreadPool.h.

References _FILE_AND_LINE_.

template<class InputType , class OutputType >
void ThreadPool< InputType, OutputType >::Clear ( void  )

Clears internal buffers.

Definition at line 477 of file ThreadPool.h.

References _FILE_AND_LINE_.

template<class InputType , class OutputType >
void ThreadPool< InputType, OutputType >::ClearInput ( void  )

Removes all items from the input queue.

Definition at line 551 of file ThreadPool.h.

References _FILE_AND_LINE_.

template<class InputType , class OutputType >
void ThreadPool< InputType, OutputType >::ClearOutput ( void  )

Removes all items from the output queue.

Definition at line 558 of file ThreadPool.h.

References _FILE_AND_LINE_.

template<class InputType , class OutputType >
InputType ThreadPool< InputType, OutputType >::GetInputAtIndex ( unsigned  index)

Get the input at a specified index.

Definition at line 515 of file ThreadPool.h.

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

Definition at line 467 of file ThreadPool.h.

template<class InputType , class OutputType >
OutputType ThreadPool< InputType, OutputType >::GetOutputAtIndex ( unsigned  index)

Get the output at a specified index.

Definition at line 541 of file ThreadPool.h.

template<class InputType , class OutputType >
bool ThreadPool< InputType, OutputType >::HasInput ( void  )

Returns true if input from GetInput is waiting.

Returns
true if input is waiting, false otherwise

Definition at line 458 of file ThreadPool.h.

template<class InputType , class OutputType >
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.

Returns
true if input is probably waiting, false otherwise

Definition at line 453 of file ThreadPool.h.

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

Definition at line 444 of file ThreadPool.h.

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

Definition at line 439 of file ThreadPool.h.

template<class InputType , class OutputType >
unsigned ThreadPool< InputType, OutputType >::InputSize ( void  )

Length of the input queue.

Definition at line 510 of file ThreadPool.h.

template<class InputType , class OutputType >
bool ThreadPool< InputType, OutputType >::IsWorking ( void  )

Are any of the threads working, or is input or output available?

Definition at line 563 of file ThreadPool.h.

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

Definition at line 500 of file ThreadPool.h.

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

Definition at line 526 of file ThreadPool.h.

template<class InputType , class OutputType >
int ThreadPool< InputType, OutputType >::NumThreadsWorking ( void  )

The number of currently active threads.

Definition at line 591 of file ThreadPool.h.

template<class InputType , class OutputType >
unsigned ThreadPool< InputType, OutputType >::OutputSize ( void  )

Length of the output queue.

Definition at line 536 of file ThreadPool.h.

template<class InputType , class OutputType >
bool ThreadPool< InputType, OutputType >::Pause ( void  )

Definition at line 606 of file ThreadPool.h.

References RakSleep().

template<class InputType, class OutputType>
template<class ThreadInputType , class ThreadOutputType >
ThreadPool< InputType, OutputType >::RAK_THREAD_DECLARATION ( WorkerThread  )
protected
template<class InputType , class OutputType >
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 520 of file ThreadPool.h.

template<class InputType , class OutputType >
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 546 of file ThreadPool.h.

template<class InputType , class OutputType >
void ThreadPool< InputType, OutputType >::Resume ( void  )

Definition at line 619 of file ThreadPool.h.

template<class InputType , class OutputType >
void ThreadPool< InputType, OutputType >::SetThreadDataInterface ( ThreadDataInterface tdi,
void *  context 
)

Definition at line 382 of file ThreadPool.h.

template<class InputType , class OutputType >
bool ThreadPool< InputType, OutputType >::StartThreads ( int  numThreads,
int  stackSize,
void *(*)()  _perThreadInit = 0,
void(*)(void *)  _perThreadDeinit = 0 
)

Start the specified number of threads.

Parameters
[in]numThreadsThe number of threads to start
[in]stackSize0 for default (except on consoles).
[in]_perThreadInitUser callback to return data stored per thread. Pass 0 if not needed.
[in]_perThreadDeinitUser callback to destroy data stored per thread, created by _perThreadInit. Pass 0 if not needed.
Returns
True on success, false on failure.

Definition at line 323 of file ThreadPool.h.

References SLNet::RakThread::Create(), numThreadsRunning, and RakSleep().

template<class InputType , class OutputType >
void ThreadPool< InputType, OutputType >::StopThreads ( void  )

Stops all threads.

Definition at line 388 of file ThreadPool.h.

References numThreadsRunning, and RakSleep().

template<class InputType , class OutputType >
void ThreadPool< InputType, OutputType >::UnlockInput ( void  )

Unlock the input buffer after you are done with the functions InputSize, GetInputAtIndex, and RemoveInputAtIndex.

Definition at line 505 of file ThreadPool.h.

template<class InputType , class OutputType >
void ThreadPool< InputType, OutputType >::UnlockOutput ( void  )

Unlock the output buffer after you are done with the functions OutputSize, GetOutputAtIndex, and RemoveOutputAtIndex.

Definition at line 531 of file ThreadPool.h.

template<class InputType , class OutputType >
bool ThreadPool< InputType, OutputType >::WasStarted ( void  )

Did we call Start?

Definition at line 597 of file ThreadPool.h.

Member Data Documentation

template<class InputType, class OutputType>
DataStructures::Queue<OutputType (*)(InputType, bool *, void*)> ThreadPool< InputType, OutputType >::inputFunctionQueue
protected

Definition at line 161 of file ThreadPool.h.

Referenced by for().

template<class InputType, class OutputType>
DataStructures::Queue<InputType> ThreadPool< InputType, OutputType >::inputQueue
protected

Definition at line 162 of file ThreadPool.h.

Referenced by for().

template<class InputType, class OutputType>
SLNet::SimpleMutex ThreadPool< InputType, OutputType >::inputQueueMutex
protected

Definition at line 154 of file ThreadPool.h.

Referenced by for().

template<class InputType, class OutputType>
int ThreadPool< InputType, OutputType >::numThreadsRunning
protected

Definition at line 183 of file ThreadPool.h.

template<class InputType, class OutputType>
SLNet::SimpleMutex ThreadPool< InputType, OutputType >::numThreadsRunningMutex
protected

Definition at line 187 of file ThreadPool.h.

template<class InputType, class OutputType>
int ThreadPool< InputType, OutputType >::numThreadsWorking
protected

Definition at line 185 of file ThreadPool.h.

Referenced by for().

template<class InputType, class OutputType>
DataStructures::Queue<OutputType> ThreadPool< InputType, OutputType >::outputQueue
protected

Definition at line 163 of file ThreadPool.h.

Referenced by for().

template<class InputType, class OutputType>
SLNet::SimpleMutex ThreadPool< InputType, OutputType >::outputQueueMutex
protected

Definition at line 154 of file ThreadPool.h.

Referenced by for().

template<class InputType, class OutputType>
void(* ThreadPool< InputType, OutputType >::perThreadDataDestructor)(void *)
protected

Definition at line 157 of file ThreadPool.h.

template<class InputType, class OutputType>
void*(* ThreadPool< InputType, OutputType >::perThreadDataFactory)()
protected

Definition at line 156 of file ThreadPool.h.

template<class InputType, class OutputType>
SLNet::SignaledEvent ThreadPool< InputType, OutputType >::quitAndIncomingDataEvents
protected

Definition at line 189 of file ThreadPool.h.

Referenced by for().

template<class InputType, class OutputType>
bool ThreadPool< InputType, OutputType >::runThreads
protected

Definition at line 181 of file ThreadPool.h.

Referenced by for().

template<class InputType, class OutputType>
SLNet::SimpleMutex ThreadPool< InputType, OutputType >::runThreadsMutex
protected

Definition at line 154 of file ThreadPool.h.

Referenced by for().

template<class InputType, class OutputType>
void* ThreadPool< InputType, OutputType >::tdiContext
protected

Definition at line 166 of file ThreadPool.h.

template<class InputType, class OutputType>
ThreadDataInterface* ThreadPool< InputType, OutputType >::threadDataInterface
protected

Definition at line 165 of file ThreadPool.h.

template<class InputType, class OutputType>
SLNet::SimpleMutex ThreadPool< InputType, OutputType >::workingThreadCountMutex
protected

Definition at line 154 of file ThreadPool.h.

Referenced by for().


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