18 #ifndef FXWorkerThread_h 19 #define FXWorkerThread_h 88 Pool(
int numThreads = 0) : myPoolMutex(true), myRunningIndex(0), myNumFinished(0) {
89 while (numThreads > 0) {
106 for (std::vector<FXWorkerThread*>::iterator it = myWorkers.begin(); it != myWorkers.end(); ++it) {
118 myWorkers.push_back(w);
130 index = myRunningIndex % myWorkers.size();
132 myWorkers[index]->add(t);
144 myFinishedTasks.push_back(t);
150 void waitAll(
const bool deleteFinished =
true) {
152 while (myNumFinished < myRunningIndex) {
156 if (deleteFinished) {
157 for (
Task* task : myFinishedTasks) {
161 myFinishedTasks.clear();
175 return myRunningIndex - myNumFinished >= size();
183 return (
int)myWorkers.size();
193 myPoolMutex.unlock();
std::vector< FXWorkerThread * > myWorkers
the current worker threads
int myNumFinished
the number of finished tasks (is reset when the pool runs empty)
void waitAll(const bool deleteFinished=true)
waits for all tasks to be finished
virtual ~FXWorkerThread()
Destructor.
FXCondition myCondition
the semaphore when waiting for new tasks
std::list< Task * > myTasks
the list of pending tasks
int myRunningIndex
the running index for the next task
FXWorkerThread(Pool &pool)
Constructor.
virtual ~Pool()
Destructor.
void addFinished(Task *const t)
Adds the given task to the list of finished tasks and assigns it to a randomly chosen worker...
void add(Task *t)
Adds the given task to this thread to be calculated.
FXMutex myMutex
the internal mutex for the task list
FXMutex myPoolMutex
the pool mutex for external sync
FXint run()
Main execution method of this thread.
void lock()
locks the pool mutex
bool myStopped
whether we are still running
int myIndex
the index of the task, valid only after the task has been added to the pool
int myCounter
counting completed tasks for debugging / profiling
std::list< Task * > myFinishedTasks
list of finished tasks
Pool(int numThreads=0)
Constructor.
int size() const
Returns the number of threads in the pool.
FXMutex myMutex
the mutex for the task list
void clear()
Stops and deletes all worker threads.
virtual ~Task()
Desctructor.
void add(Task *const t, int index=-1)
Gives a number to the given task and assigns it to the worker with the given index. If the index is negative, assign to the next (round robin) one.
A pool of worker threads which distributes the tasks and collects the results.
bool isFull() const
Checks whether there are currently more pending tasks than threads.
void addWorker(FXWorkerThread *const w)
Adds the given thread to the pool.
Pool & myPool
the pool for this thread
virtual void run(FXWorkerThread *context)=0
Abstract method which in subclasses should contain the computations to be performed.
void unlock()
unlocks the pool mutex
void stop()
Stops the thread.
Abstract superclass of a task to be run with an index to keep track of pending tasks.
A thread repeatingly calculating incoming tasks.
FXCondition myCondition
the semaphore to wait on for finishing all tasks
void setIndex(const int newIndex)
Sets the running index of this task.