A class used to execute tasks in a separated threads. More...
#include <Radiant/BGThread.hpp>
Public Types | |
|
typedef std::pair< Priority, TaskPtr > | contained |
| Objects stored in the task container. | |
|
typedef std::multimap < Priority, TaskPtr, std::greater< Priority > > | container |
| Container for the tasks. | |
Public Member Functions | |
| virtual void | addTask (TaskPtr task) |
| Add a task to be executed. More... | |
| void | dumpInfo (FILE *f=0, int indent=0) |
| Dump information about the tasks at hand. More... | |
| unsigned int | overdueTasks () const |
| Get the number of tasks that should be running right now but are not yet processed. More... | |
| virtual bool | removeTask (TaskPtr task, bool cancel=true, bool wait=false) |
| Remove the task from the BGThread. More... | |
| virtual void | reschedule (TaskPtr task) |
| Notify the BGThread that a task execution time has been rescheduled. More... | |
| void | reschedule (TaskPtr task, Priority p) |
| Notify the BGThread that a task execution time has been rescheduled. More... | |
| unsigned int | runningTasks () const |
| Get the number of tasks right now in doTask(). More... | |
| virtual void | setPriority (TaskPtr task, Priority p) |
| Change the priority of a task. More... | |
| void | shutdown () |
| Prepare the BGThread for shutdown. More... | |
| unsigned | taskCount () |
| Returns the number of tasks in the BGThread. More... | |
Public Member Functions inherited from Radiant::ThreadPool | |
| bool | isRunning () const |
| Returns true if any of the threads are running. More... | |
| void | run (int number=1) |
| Sets the number of threads. More... | |
| bool | stop () |
| Asks threads to stop. More... | |
| ThreadPool () | |
| Construct a thread pool class. More... | |
| int | threads () const |
| Returns the number of running or starting threads. More... | |
| bool | waitEnd () |
| Waits until all threads are finished. More... | |
| virtual | ~ThreadPool () |
| Destructor. More... | |
Additional Inherited Members | |
Protected Member Functions inherited from Radiant::ThreadPool | |
| bool | running () const |
| This should only be called from childLoop(), This function is thread-safe. More... | |
Protected Attributes inherited from Radiant::ThreadPool | |
| Radiant::Mutex | m_mutexWait |
| Mutex to be used with m_wait. | |
| Radiant::Condition | m_wait |
| Every time when we want to delete a thread, this condition variable will signaled. More... | |
A class used to execute tasks in a separated threads.
BGThread implements a thread-pool of one or more threads that are used to execute simple tasks that take too much time to be performed in the main-thread. Typical use-cases are generating mip-maps and converting images, loading large resources from disk or database, streaming resources over network, etc.
BGThread owns tasks added to it and handles their destruction and memory management for you. If you need to keep a pointer to a task in BGThread, you should use the shared_ptr returned by Radiant::BGThread::addTask.
If you decide to hold an external pointer to any Radiant::Task running in a BGThread, take special care if you decide to modify the task outside. You may not know if the Task is currently being executed in another thread.
It is possible to change the number of threads executing tasks on the fly in BGThread by using the Radiant::ThreadPool::run function.
|
virtual |
Add a task to be executed.
The task is the property of the BGThread, which will delete the object when its operation is finished and the shared pointer's reference count goes to zero.
| task | The task that needs to be added. |
| void Radiant::BGThread::dumpInfo | ( | FILE * | f = 0, |
| int | indent = 0 |
||
| ) |
Dump information about the tasks at hand.
| f | File handle for printing. If null this will print to stdout |
| indent | Default intendation level, used internally in recursive calls |
| unsigned int Radiant::BGThread::overdueTasks | ( | ) | const |
Get the number of tasks that should be running right now but are not yet processed.
This function is slow: O(N), needs a mutex lock and calls TimeStamp::currentTime().
|
virtual |
Remove the task from the BGThread.
Generally you should not use this function. If you want to remove/delete a task, you set its state to finished (Radiant::Task::setFinished) and schedule it for immediate processing after which BGThread will remove it when it has a chance.
| task | The task to be removed |
| cancel | Should the task be cancelled if succesfully removed |
| wait | Block until the task execution returns, if the task is currently running |
|
virtual |
Notify the BGThread that a task execution time has been rescheduled.
This function should always be called after modifying the time-stamp of a task.
| task | task to reschedule |
Notify the BGThread that a task execution time has been rescheduled.
This function should always be called after modifying the time-stamp of a task.
| task | task to reschedule |
| unsigned int Radiant::BGThread::runningTasks | ( | ) | const |
Get the number of tasks right now in doTask().
This function is lock-free and O(1).
Change the priority of a task.
| task | task to modify |
| p | task priority |
| void Radiant::BGThread::shutdown | ( | ) |
| unsigned Radiant::BGThread::taskCount | ( | ) |
Returns the number of tasks in the BGThread.
This includes queued tasks and currently running tasks.