Code::Blocks
SVN r11506
|
A Thread Pool implementation. More...
#include <cbthreadpool.h>
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< cbThreadedTaskElement > | TasksQueue |
Private Member Functions | |
void | _SetConcurrentThreads (int concurrentThreads) |
void | Broadcast () |
void | AwakeNeeded () |
Private Attributes | |
wxEvtHandler * | m_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< wxSemaphore > | m_semaphore |
Friends | |
class | cbWorkerThread |
A Thread Pool implementation.
Definition at line 19 of file cbthreadpool.h.
|
private |
Definition at line 233 of file cbthreadpool.h.
|
private |
Definition at line 207 of file cbthreadpool.h.
|
inline |
cbThreadPool ctor
owner | Event handler to receive cbEVT_THREADTASK_ENDED and cbEVT_THREADTASK_ALLDONE events |
id | Used with the events |
concurrentThreads | Number of threads in the pool. -1 means current CPU count |
Definition at line 324 of file cbthreadpool.h.
References SetConcurrentThreads().
cbThreadPool::~cbThreadPool | ( | ) |
cbThreadPool dtor
Definition at line 22 of file cbthreadpool.cpp.
References cbThreadPool::cbWorkerThread::Abort(), Broadcast(), cbThreadPool::cbThreadedTaskElement::Delete(), m_Mutex, m_tasksQueue, and m_threads.
|
private |
Definition at line 52 of file cbthreadpool.cpp.
References cbThreadPool::cbWorkerThread::Abort(), Broadcast(), cbWorkerThread, m_concurrentThreads, m_concurrentThreadsSchedule, m_semaphore, m_stackSize, m_threads, and m_workingThreads.
Referenced by SetConcurrentThreads(), and WaitingThread().
void cbThreadPool::AbortAllTasks | ( | ) |
Aborts all running and pending tasks.
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().
void cbThreadPool::AddTask | ( | cbThreadedTask * | task, |
bool | autodelete = true |
||
) |
Adds a new task to the pool.
task | The task to execute |
autodelete | If 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().
|
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().
|
inline |
Begin a batch process.
Definition at line 351 of file cbthreadpool.h.
References m_batching, and m_Mutex.
void cbThreadPool::BatchEnd | ( | ) |
End a batch process.
Definition at line 104 of file cbthreadpool.cpp.
References AwakeNeeded(), m_batching, and m_Mutex.
|
inlineprivate |
Definition at line 357 of file cbthreadpool.h.
References m_concurrentThreads, m_semaphore, m_workingThreads, and wxSemaphore::Post().
Referenced by _SetConcurrentThreads(), and ~cbThreadPool().
|
inline |
Tells if the pool has finished its job.
Definition at line 345 of file cbthreadpool.h.
References m_Mutex, and m_workingThreads.
Referenced by Parser::Done(), Parser::NotDoneReason(), and Parser::TerminateAllThreads().
|
inline |
Gets the current number of threads in the pool.
Definition at line 339 of file cbthreadpool.h.
References m_concurrentThreads, m_concurrentThreadsSchedule, and m_Mutex.
|
inline |
return the pool ID
Definition at line 49 of file cbthreadpool.h.
Referenced by Parser::ConnectEvents(), Parser::DisconnectEvents(), and Parser::OnAllThreadsDone().
|
protected |
Returns the next task in the queue to run.
Definition at line 112 of file cbthreadpool.cpp.
References m_Mutex, and m_tasksQueue.
Referenced by cbThreadPool::cbWorkerThread::Entry().
void cbThreadPool::SetConcurrentThreads | ( | int | concurrentThreads | ) |
Changes the number of threads in the pool.
concurrentThreads | New number of threads. -1 or 0 means current CPU count |
Definition at line 32 of file cbthreadpool.cpp.
References _SetConcurrentThreads(), wxThread::GetCPUCount(), m_concurrentThreads, m_concurrentThreadsSchedule, and m_Mutex.
Referenced by cbThreadPool().
|
protected |
Called by a Worker Thread to inform a single task has finished, this will send a cbEVT_THREADTASK_ENDED event.
thread | The 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().
|
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
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().
|
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().
|
friend |
Definition at line 289 of file cbthreadpool.h.
Referenced by _SetConcurrentThreads().
|
private |
Definition at line 237 of file cbthreadpool.h.
Referenced by AddTask(), BatchBegin(), and BatchEnd().
|
private |
Definition at line 241 of file cbthreadpool.h.
Referenced by _SetConcurrentThreads(), AddTask(), AwakeNeeded(), Broadcast(), GetConcurrentThreads(), and SetConcurrentThreads().
|
private |
Definition at line 246 of file cbthreadpool.h.
Referenced by _SetConcurrentThreads(), GetConcurrentThreads(), SetConcurrentThreads(), and WaitingThread().
|
private |
Definition at line 236 of file cbthreadpool.h.
Referenced by TaskDone(), and WaitingThread().
|
mutableprivate |
Definition at line 266 of file cbthreadpool.h.
Referenced by AbortAllTasks(), AddTask(), BatchBegin(), BatchEnd(), Done(), GetConcurrentThreads(), GetNextTask(), SetConcurrentThreads(), WaitingThread(), WorkingThread(), and ~cbThreadPool().
|
private |
Definition at line 235 of file cbthreadpool.h.
Referenced by TaskDone(), and WaitingThread().
|
private |
Definition at line 273 of file cbthreadpool.h.
Referenced by _SetConcurrentThreads(), AwakeNeeded(), Broadcast(), and WaitingThread().
|
private |
Definition at line 242 of file cbthreadpool.h.
Referenced by _SetConcurrentThreads().
|
private |
Definition at line 259 of file cbthreadpool.h.
Referenced by AddTask(), and WaitingThread().
|
private |
Definition at line 256 of file cbthreadpool.h.
Referenced by AbortAllTasks(), AddTask(), AwakeNeeded(), GetNextTask(), WaitingThread(), and ~cbThreadPool().
|
private |
Definition at line 250 of file cbthreadpool.h.
Referenced by _SetConcurrentThreads(), AbortAllTasks(), and ~cbThreadPool().
|
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().