Code::Blocks  SVN r11506
Classes | Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Private Attributes | Friends | List of all members
cbThreadPool Class Reference

A Thread Pool implementation. More...

#include <cbthreadpool.h>

Collaboration diagram for cbThreadPool:

Classes

struct  cbThreadedTaskElement
 All tasks are added to one of these. It'll also save the autodelete value. More...
 
class  cbWorkerThread
 A Worker Thread class. More...
 
class  CountedPtr
 Josuttis' implementation of CountedPtr. More...
 

Public Member Functions

 cbThreadPool (wxEvtHandler *owner, int id=-1, int concurrentThreads=-1, unsigned int stackSize=0)
 cbThreadPool ctor More...
 
 ~cbThreadPool ()
 cbThreadPool dtor More...
 
void SetConcurrentThreads (int concurrentThreads)
 Changes the number of threads in the pool. More...
 
int GetConcurrentThreads () const
 Gets the current number of threads in the pool. More...
 
int GetId () const
 return the pool ID More...
 
void AddTask (cbThreadedTask *task, bool autodelete=true)
 Adds a new task to the pool. More...
 
void AbortAllTasks ()
 Aborts all running and pending tasks. More...
 
bool Done () const
 Tells if the pool has finished its job. More...
 
void BatchBegin ()
 Begin a batch process. More...
 
void BatchEnd ()
 End a batch process. More...
 

Protected Member Functions

cbThreadedTaskElement GetNextTask ()
 Returns the next task in the queue to run. More...
 
void WorkingThread ()
 Mechanism for the threads to tell the Pool they're running, a thread is switch from the idle mode to working mode. More...
 
bool WaitingThread ()
 Mechanism for the threads to tell the Pool they're done and will go to idle, so we can assign another task to this thread. More...
 
void TaskDone (cbWorkerThread *thread)
 Called by a Worker Thread to inform a single task has finished, this will send a cbEVT_THREADTASK_ENDED event. More...
 

Private Types

typedef std::vector< cbWorkerThread * > WorkerThreadsArray
 
typedef std::list< cbThreadedTaskElementTasksQueue
 

Private Member Functions

void _SetConcurrentThreads (int concurrentThreads)
 
void Broadcast ()
 
void AwakeNeeded ()
 

Private Attributes

wxEvtHandlerm_pOwner
 
int m_ID
 
bool m_batching
 
int m_concurrentThreads
 
unsigned int m_stackSize
 
int m_concurrentThreadsSchedule
 
WorkerThreadsArray m_threads
 
TasksQueue m_tasksQueue
 
bool m_taskAdded
 
int m_workingThreads
 how many working threads are running tasks m_workingThreads + thread in Idle = m_concurrentThreads More...
 
wxMutex m_Mutex
 
CountedPtr< wxSemaphorem_semaphore
 

Friends

class cbWorkerThread
 

Detailed Description

A Thread Pool implementation.

Definition at line 19 of file cbthreadpool.h.

Member Typedef Documentation

◆ TasksQueue

typedef std::list<cbThreadedTaskElement> cbThreadPool::TasksQueue
private

Definition at line 233 of file cbthreadpool.h.

◆ WorkerThreadsArray

typedef std::vector<cbWorkerThread *> cbThreadPool::WorkerThreadsArray
private

Definition at line 207 of file cbthreadpool.h.

Constructor & Destructor Documentation

◆ cbThreadPool()

cbThreadPool::cbThreadPool ( wxEvtHandler owner,
int  id = -1,
int  concurrentThreads = -1,
unsigned int  stackSize = 0 
)
inline

cbThreadPool ctor

Parameters
ownerEvent handler to receive cbEVT_THREADTASK_ENDED and cbEVT_THREADTASK_ALLDONE events
idUsed with the events
concurrentThreadsNumber of threads in the pool. -1 means current CPU count

Definition at line 324 of file cbthreadpool.h.

References SetConcurrentThreads().

◆ ~cbThreadPool()

cbThreadPool::~cbThreadPool ( )

Member Function Documentation

◆ _SetConcurrentThreads()

void cbThreadPool::_SetConcurrentThreads ( int  concurrentThreads)
private

◆ AbortAllTasks()

void cbThreadPool::AbortAllTasks ( )

Aborts all running and pending tasks.

Note
Calls cbThreadedTask::Abort for all running tasks and just removes the pending ones.

Definition at line 95 of file cbthreadpool.cpp.

References cbThreadPool::cbWorkerThread::AbortTask(), cbThreadPool::cbThreadedTaskElement::Delete(), m_Mutex, m_tasksQueue, and m_threads.

Referenced by Parser::TerminateAllThreads().

◆ AddTask()

void cbThreadPool::AddTask ( cbThreadedTask task,
bool  autodelete = true 
)

Adds a new task to the pool.

Parameters
taskThe task to execute
autodeleteIf true, the task will be deleted when it finishes or be aborted

Definition at line 79 of file cbthreadpool.cpp.

References AwakeNeeded(), m_batching, m_concurrentThreads, m_Mutex, m_taskAdded, m_tasksQueue, and m_workingThreads.

Referenced by Parser::OnAllThreadsDone(), Parser::OnBatchTimer(), and Parser::Parse().

◆ AwakeNeeded()

void cbThreadPool::AwakeNeeded ( )
inlineprivate

Definition at line 367 of file cbthreadpool.h.

References m_concurrentThreads, m_semaphore, m_tasksQueue, m_workingThreads, and wxSemaphore::Post().

Referenced by AddTask(), and BatchEnd().

◆ BatchBegin()

void cbThreadPool::BatchBegin ( )
inline

Begin a batch process.

Note
EVIL: Call it if you want to add all tasks first and get none executed yet. If you DON'T call it, tasks will be executed as you add them (in fact it's what one would expect).
If when calling the BatchBegin(), the pool is ready running tasks, then the added task still has chance to run, so be best time to call this function is Done() returns true

Definition at line 351 of file cbthreadpool.h.

References m_batching, and m_Mutex.

◆ BatchEnd()

void cbThreadPool::BatchEnd ( )

End a batch process.

Note
EVIL: Call it when you have finished adding tasks and want them to execute. BEWARE: if you call BatchBegin but DON'T call BatchEnd, the tasks WON'T execute.

Definition at line 104 of file cbthreadpool.cpp.

References AwakeNeeded(), m_batching, and m_Mutex.

◆ Broadcast()

void cbThreadPool::Broadcast ( )
inlineprivate

◆ Done()

bool cbThreadPool::Done ( ) const
inline

Tells if the pool has finished its job.

Returns
true if it has nothing to do, false if it's executing tasks

Definition at line 345 of file cbthreadpool.h.

References m_Mutex, and m_workingThreads.

Referenced by Parser::Done(), Parser::NotDoneReason(), and Parser::TerminateAllThreads().

◆ GetConcurrentThreads()

int cbThreadPool::GetConcurrentThreads ( ) const
inline

Gets the current number of threads in the pool.

Returns
Number of threads in the pool
Note
If a call to SetConcurrentThreads hasn't been applied, it'll return the number of threads that will be set by it when all tasks be done.

Definition at line 339 of file cbthreadpool.h.

References m_concurrentThreads, m_concurrentThreadsSchedule, and m_Mutex.

◆ GetId()

int cbThreadPool::GetId ( ) const
inline

return the pool ID

Definition at line 49 of file cbthreadpool.h.

Referenced by Parser::ConnectEvents(), Parser::DisconnectEvents(), and Parser::OnAllThreadsDone().

◆ GetNextTask()

cbThreadPool::cbThreadedTaskElement cbThreadPool::GetNextTask ( )
protected

Returns the next task in the queue to run.

Returns
Next task to run, or a NULL task (set in .task) if none

Definition at line 112 of file cbthreadpool.cpp.

References m_Mutex, and m_tasksQueue.

Referenced by cbThreadPool::cbWorkerThread::Entry().

◆ SetConcurrentThreads()

void cbThreadPool::SetConcurrentThreads ( int  concurrentThreads)

Changes the number of threads in the pool.

Parameters
concurrentThreadsNew number of threads. -1 or 0 means current CPU count
Note
If tasks are running, it'll delay it until they're all done.

Definition at line 32 of file cbthreadpool.cpp.

References _SetConcurrentThreads(), wxThread::GetCPUCount(), m_concurrentThreads, m_concurrentThreadsSchedule, and m_Mutex.

Referenced by cbThreadPool().

◆ TaskDone()

void cbThreadPool::TaskDone ( cbWorkerThread thread)
protected

Called by a Worker Thread to inform a single task has finished, this will send a cbEVT_THREADTASK_ENDED event.

Parameters
threadThe Worker Thread

Definition at line 162 of file cbthreadpool.cpp.

References cbEVT_THREADTASK_ENDED, m_ID, m_pOwner, and wxPostEvent().

Referenced by cbThreadPool::cbWorkerThread::Entry().

◆ WaitingThread()

bool cbThreadPool::WaitingThread ( )
protected

Mechanism for the threads to tell the Pool they're done and will go to idle, so we can assign another task to this thread.

this function will be called in the worker thread, it just say: hey, I have finished one task thus, decrease the running thread number by one, and let me go to idle mode

Returns
true if everything is OK, false if we should abort, this usually happens we need to set a scheduled m_concurrentThreads value.

Definition at line 133 of file cbthreadpool.cpp.

References _SetConcurrentThreads(), cbEVT_THREADTASK_ALLDONE, m_concurrentThreadsSchedule, m_ID, m_Mutex, m_pOwner, m_semaphore, m_taskAdded, m_tasksQueue, m_workingThreads, wxSemaphore::Post(), and wxPostEvent().

Referenced by cbThreadPool::cbWorkerThread::Entry().

◆ WorkingThread()

void cbThreadPool::WorkingThread ( )
protected

Mechanism for the threads to tell the Pool they're running, a thread is switch from the idle mode to working mode.

This is triggered by semaphore released somewhere this function will be called in the worker thread, the thread just say: hey, I'm running now so increase the running thread number by one

Definition at line 125 of file cbthreadpool.cpp.

References m_Mutex, and m_workingThreads.

Referenced by cbThreadPool::cbWorkerThread::Entry().

Friends And Related Function Documentation

◆ cbWorkerThread

friend class cbWorkerThread
friend

Definition at line 289 of file cbthreadpool.h.

Referenced by _SetConcurrentThreads().

Member Data Documentation

◆ m_batching

bool cbThreadPool::m_batching
private

Definition at line 237 of file cbthreadpool.h.

Referenced by AddTask(), BatchBegin(), and BatchEnd().

◆ m_concurrentThreads

int cbThreadPool::m_concurrentThreads
private

◆ m_concurrentThreadsSchedule

int cbThreadPool::m_concurrentThreadsSchedule
private

◆ m_ID

int cbThreadPool::m_ID
private

Definition at line 236 of file cbthreadpool.h.

Referenced by TaskDone(), and WaitingThread().

◆ m_Mutex

wxMutex cbThreadPool::m_Mutex
mutableprivate

◆ m_pOwner

wxEvtHandler* cbThreadPool::m_pOwner
private

Definition at line 235 of file cbthreadpool.h.

Referenced by TaskDone(), and WaitingThread().

◆ m_semaphore

CountedPtr<wxSemaphore> cbThreadPool::m_semaphore
private

Definition at line 273 of file cbthreadpool.h.

Referenced by _SetConcurrentThreads(), AwakeNeeded(), Broadcast(), and WaitingThread().

◆ m_stackSize

unsigned int cbThreadPool::m_stackSize
private

Definition at line 242 of file cbthreadpool.h.

Referenced by _SetConcurrentThreads().

◆ m_taskAdded

bool cbThreadPool::m_taskAdded
private

Definition at line 259 of file cbthreadpool.h.

Referenced by AddTask(), and WaitingThread().

◆ m_tasksQueue

TasksQueue cbThreadPool::m_tasksQueue
private

◆ m_threads

WorkerThreadsArray cbThreadPool::m_threads
private

Definition at line 250 of file cbthreadpool.h.

Referenced by _SetConcurrentThreads(), AbortAllTasks(), and ~cbThreadPool().

◆ m_workingThreads

int cbThreadPool::m_workingThreads
private

how many working threads are running tasks m_workingThreads + thread in Idle = m_concurrentThreads

Definition at line 264 of file cbthreadpool.h.

Referenced by _SetConcurrentThreads(), AddTask(), AwakeNeeded(), Broadcast(), Done(), WaitingThread(), and WorkingThread().


The documentation for this class was generated from the following files: