C++ common classes
General purpose classes for use across projects (Apache license)
|
Implementation of thread pool class. More...
#include <CXPlatThreadPool.h>
Public Types | |
enum | { TP_REQUEST_EXCEEDS_POOL_SIZE = -1, TP_NOT_ENOUGH_THREADS_AVAILABLE = -2, TP_ZERO_THREADS_REQUESTED = -3, TP_NO_THREADS_IN_POOL = -4, TP_WAIT_TIMED_OUT = -5 } |
Public Member Functions | |
CXPlatThreadPool (int iPoolSize=5, int iMaxSize=0, int iGrowBy=1) | |
Initial poolsize, maximum size to grow to, size to grow by when pool is grown. | |
virtual | ~CXPlatThreadPool () |
int | GetAvailableThreads (THREAD_POOL_ARRAY &cAvailThreadPool, int iNeeded, BOOL bWaitForAtLeastOne=TRUE, DWORD dwWaitTime=INFINITE) |
int | QueryThreadsAvailable () |
int | GetCurrentSizeOfPool () |
Get how many threads are in the pool. | |
int | GetMaxSize () |
Get the maximum size the thread pool can grow to. | |
void | SetMaxSize (int iMaxSize) |
Set the maximum size the thread pool can grow to. | |
void | WaitForAllThreadsToFinish () |
Wait for all threads to finish. Do NOT call GetAvailableThreads at the same time. | |
void | ShrinkPool () |
Shrink the thread pool by one thread. | |
void | SetFinishWaitTime (unsigned long lWaitTime) |
Set the wait time when we are waiting for all threads to finish. | |
Protected Member Functions | |
virtual CXPlatPooledThread * | AllocatePooledThread ()=0 |
Implement to allocate the proper type of derived pooled thread. | |
BOOL | GrowThreadPool (BOOL bInit=FALSE) |
Add threads to the pool. | |
BOOL | Init () |
Protected Attributes | |
THREAD_POOL_ARRAY | cThreadPool |
CXPlatCriticalSection | cPoolCritSect |
Our pool of threads. | |
CXPlatEvent | cThreadAvailableEvent |
To protect ThreadPool. | |
int | iOriginalThreadsInPool |
Threads will set when they become available. | |
int | iThreadsInPool |
int | iMaxThreadsAllowed |
int | iGrowBy |
bool | bShrinking |
unsigned long | lWaitToFinish |
bool | bWaitToFinishSet |
Friends | |
class | CXPlatPooledThread |
Implementation of thread pool class.
Definition at line 37 of file CXPlatThreadPool.h.
anonymous enum |
TP_REQUEST_EXCEEDS_POOL_SIZE | |
TP_NOT_ENOUGH_THREADS_AVAILABLE | |
TP_ZERO_THREADS_REQUESTED | |
TP_NO_THREADS_IN_POOL | |
TP_WAIT_TIMED_OUT |
Definition at line 46 of file CXPlatThreadPool.h.
CXPlatThreadPool::CXPlatThreadPool | ( | int | iPoolSize = 5 , |
int | iMaxSize = 0 , |
||
int | iGrowBy = 1 |
||
) |
Initial poolsize, maximum size to grow to, size to grow by when pool is grown.
CXPlatThreadPool.cpp: implementation of the CXPlatThreadPool class.
This class manages a pool of CXPlatPooledThread objects
Definition at line 31 of file CXPlatThreadPool.cpp.
CXPlatThreadPool::~CXPlatThreadPool | ( | ) | [virtual] |
Definition at line 51 of file CXPlatThreadPool.cpp.
virtual CXPlatPooledThread* CXPlatThreadPool::AllocatePooledThread | ( | ) | [protected, pure virtual] |
Implement to allocate the proper type of derived pooled thread.
int CXPlatThreadPool::GetAvailableThreads | ( | THREAD_POOL_ARRAY & | cAvailThreadPool, |
int | iNeeded, | ||
BOOL | bWaitForAtLeastOne = TRUE , |
||
DWORD | dwWaitTime = INFINITE |
||
) |
Array will be set with available thread objects.
If the return is > 0, it's the number of threads returned. A return of < 0 will be one of the above error enums.
If bWaitForAtLeastOne == TRUE, at least one thread must become available or the call will wait dwWaitTime ms before returning.
Threads returned will be marked as allocated until the CXPlatPooledThread::PooledWorkerFunction returns. If CXPlatPooledThread::StartWorking is never called, this thread will never become available again.
Definition at line 138 of file CXPlatThreadPool.cpp.
Get how many threads are in the pool.
Definition at line 296 of file CXPlatThreadPool.cpp.
int CXPlatThreadPool::GetMaxSize | ( | ) |
Get the maximum size the thread pool can grow to.
Definition at line 311 of file CXPlatThreadPool.cpp.
BOOL CXPlatThreadPool::GrowThreadPool | ( | BOOL | bInit = FALSE | ) | [protected] |
Add threads to the pool.
Definition at line 82 of file CXPlatThreadPool.cpp.
BOOL CXPlatThreadPool::Init | ( | ) | [protected] |
Derived classes should call this to initial the thread pool Can be called in the constructor
Definition at line 71 of file CXPlatThreadPool.cpp.
How many threads are currently unallocated by requests? Note that you cannot depend on these threads remaining available if there is more than one than one thread of execution in your program that dispatches the threads.
Definition at line 266 of file CXPlatThreadPool.cpp.
void CXPlatThreadPool::SetFinishWaitTime | ( | unsigned long | lWaitTime | ) |
Set the wait time when we are waiting for all threads to finish.
Definition at line 333 of file CXPlatThreadPool.cpp.
void CXPlatThreadPool::SetMaxSize | ( | int | iMaxSize | ) |
Set the maximum size the thread pool can grow to.
Definition at line 318 of file CXPlatThreadPool.cpp.
Shrink the thread pool by one thread.
Definition at line 114 of file CXPlatThreadPool.cpp.
Wait for all threads to finish. Do NOT call GetAvailableThreads at the same time.
Definition at line 324 of file CXPlatThreadPool.cpp.
friend class CXPlatPooledThread [friend] |
Definition at line 39 of file CXPlatThreadPool.h.
bool CXPlatThreadPool::bShrinking [protected] |
Definition at line 104 of file CXPlatThreadPool.h.
bool CXPlatThreadPool::bWaitToFinishSet [protected] |
Definition at line 107 of file CXPlatThreadPool.h.
CXPlatCriticalSection CXPlatThreadPool::cPoolCritSect [protected] |
Our pool of threads.
Definition at line 97 of file CXPlatThreadPool.h.
CXPlatEvent CXPlatThreadPool::cThreadAvailableEvent [protected] |
To protect ThreadPool.
Definition at line 98 of file CXPlatThreadPool.h.
THREAD_POOL_ARRAY CXPlatThreadPool::cThreadPool [protected] |
Definition at line 96 of file CXPlatThreadPool.h.
int CXPlatThreadPool::iGrowBy [protected] |
Definition at line 103 of file CXPlatThreadPool.h.
int CXPlatThreadPool::iMaxThreadsAllowed [protected] |
Definition at line 102 of file CXPlatThreadPool.h.
int CXPlatThreadPool::iOriginalThreadsInPool [protected] |
Threads will set when they become available.
Shared event, a thread will reset when it becomes available
Definition at line 100 of file CXPlatThreadPool.h.
int CXPlatThreadPool::iThreadsInPool [protected] |
Definition at line 101 of file CXPlatThreadPool.h.
unsigned long CXPlatThreadPool::lWaitToFinish [protected] |
Definition at line 105 of file CXPlatThreadPool.h.