16 #ifndef __THREAD_POOL_H
17 #define __THREAD_POOL_H
39 template <
class InputType,
class OutputType>
51 bool StartThreads(
int numThreads,
int stackSize,
void* (*_perThreadInit)()=0,
void (*_perThreadDeinit)(
void*)=0);
57 void StopThreads(
void);
72 void AddOutput(OutputType outputData);
80 bool HasOutputFast(
void);
88 bool HasInputFast(
void);
93 OutputType GetOutput(
void);
100 void LockInput(
void);
103 void UnlockInput(
void);
106 unsigned InputSize(
void);
109 InputType GetInputAtIndex(
unsigned index);
112 void RemoveInputAtIndex(
unsigned index);
116 void LockOutput(
void);
119 void UnlockOutput(
void);
122 unsigned OutputSize(
void);
125 OutputType GetOutputAtIndex(
unsigned index);
128 void RemoveOutputAtIndex(
unsigned index);
131 void ClearInput(
void);
134 void ClearOutput(
void);
137 bool IsWorking(
void);
140 int NumThreadsWorking(
void);
143 bool WasStarted(
void);
156 void* (*perThreadDataFactory)();
157 void (*perThreadDataDestructor)(
void*);
169 template <
class ThreadInputType,
class ThreadOutputType>
204 template <
class ThreadInputType,
class ThreadOutputType>
276 callbackOutput=
userCallback(inputData, &returnOutput,perThreadData);
306 template <
class InputType,
class OutputType>
311 threadDataInterface=0;
316 template <
class InputType,
class OutputType>
322 template <
class InputType,
class OutputType>
331 runThreadsMutex.Lock();
332 if (runThreads==
true)
335 runThreadsMutex.Unlock();
338 runThreadsMutex.Unlock();
340 quitAndIncomingDataEvents.InitEvent();
342 perThreadDataFactory=_perThreadDataFactory;
343 perThreadDataDestructor=_perThreadDataDestructor;
345 runThreadsMutex.Lock();
347 runThreadsMutex.Unlock();
350 unsigned threadId = 0;
353 for (i=0; i < numThreads; i++)
373 numThreadsRunningMutex.Lock();
376 numThreadsRunningMutex.Unlock();
381 template <
class InputType,
class OutputType>
384 threadDataInterface=tdi;
387 template <
class InputType,
class OutputType>
390 runThreadsMutex.Lock();
391 if (runThreads==
false)
393 runThreadsMutex.Unlock();
398 runThreadsMutex.Unlock();
404 quitAndIncomingDataEvents.SetEvent();
407 numThreadsRunningMutex.Lock();
410 numThreadsRunningMutex.Unlock();
413 quitAndIncomingDataEvents.CloseEvent();
421 template <
class InputType,
class OutputType>
424 inputQueueMutex.Lock();
427 inputQueueMutex.Unlock();
429 quitAndIncomingDataEvents.SetEvent();
431 template <
class InputType,
class OutputType>
434 outputQueueMutex.Lock();
436 outputQueueMutex.Unlock();
438 template <
class InputType,
class OutputType>
441 return outputQueue.IsEmpty()==
false;
443 template <
class InputType,
class OutputType>
447 outputQueueMutex.Lock();
448 res=outputQueue.IsEmpty()==
false;
449 outputQueueMutex.Unlock();
452 template <
class InputType,
class OutputType>
455 return inputQueue.IsEmpty()==
false;
457 template <
class InputType,
class OutputType>
461 inputQueueMutex.Lock();
462 res=inputQueue.IsEmpty()==
false;
463 inputQueueMutex.Unlock();
466 template <
class InputType,
class OutputType>
471 outputQueueMutex.Lock();
472 output=outputQueue.Pop();
473 outputQueueMutex.Unlock();
476 template <
class InputType,
class OutputType>
479 runThreadsMutex.Lock();
482 runThreadsMutex.Unlock();
483 inputQueueMutex.Lock();
486 inputQueueMutex.Unlock();
488 outputQueueMutex.Lock();
490 outputQueueMutex.Unlock();
499 template <
class InputType,
class OutputType>
502 inputQueueMutex.Lock();
504 template <
class InputType,
class OutputType>
507 inputQueueMutex.Unlock();
509 template <
class InputType,
class OutputType>
512 return inputQueue.Size();
514 template <
class InputType,
class OutputType>
517 return inputQueue[index];
519 template <
class InputType,
class OutputType>
522 inputQueue.RemoveAtIndex(index);
523 inputFunctionQueue.RemoveAtIndex(index);
525 template <
class InputType,
class OutputType>
528 outputQueueMutex.Lock();
530 template <
class InputType,
class OutputType>
533 outputQueueMutex.Unlock();
535 template <
class InputType,
class OutputType>
538 return outputQueue.Size();
540 template <
class InputType,
class OutputType>
543 return outputQueue[index];
545 template <
class InputType,
class OutputType>
548 outputQueue.RemoveAtIndex(index);
550 template <
class InputType,
class OutputType>
557 template <
class InputType,
class OutputType>
562 template <
class InputType,
class OutputType>
576 if (HasOutputFast() && HasOutput())
579 if (HasInputFast() && HasInput())
583 workingThreadCountMutex.Lock();
584 isWorking=numThreadsWorking!=0;
585 workingThreadCountMutex.Unlock();
590 template <
class InputType,
class OutputType>
593 return numThreadsWorking;
596 template <
class InputType,
class OutputType>
600 runThreadsMutex.Lock();
602 runThreadsMutex.Unlock();
605 template <
class InputType,
class OutputType>
608 if (WasStarted()==
false)
611 workingThreadCountMutex.Lock();
612 while (numThreadsWorking>0)
618 template <
class InputType,
class OutputType>
621 workingThreadCountMutex.Unlock();