All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::BGThread Class Reference

A class used to execute tasks in a separated threads. More...

#include <Radiant/BGThread.hpp>

Inheritance diagram for Radiant::BGThread:
Radiant::ThreadPool Patterns::NotCopyable

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...
 

Detailed Description

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.

Member Function Documentation

virtual void Radiant::BGThread::addTask ( TaskPtr  task)
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.

Parameters
taskThe task that needs to be added.
void Radiant::BGThread::dumpInfo ( FILE *  f = 0,
int  indent = 0 
)

Dump information about the tasks at hand.

Parameters
fFile handle for printing. If null this will print to stdout
indentDefault 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().

Returns
number of overdue tasks
virtual bool Radiant::BGThread::removeTask ( TaskPtr  task,
bool  cancel = true,
bool  wait = false 
)
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.

Parameters
taskThe task to be removed
cancelShould the task be cancelled if succesfully removed
waitBlock until the task execution returns, if the task is currently running
Returns
True if the task was successfully removes, false otherwise.
See Also
Radiant::Task::setFinished
Radiant::Task::schedule
virtual void Radiant::BGThread::reschedule ( TaskPtr  task)
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.

Parameters
tasktask to reschedule
void Radiant::BGThread::reschedule ( TaskPtr  task,
Priority  p 
)

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.

Parameters
tasktask 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).

Returns
number of running tasks
virtual void Radiant::BGThread::setPriority ( TaskPtr  task,
Priority  p 
)
virtual

Change the priority of a task.

Parameters
tasktask to modify
ptask priority
void Radiant::BGThread::shutdown ( )

Prepare the BGThread for shutdown.

This function will cancel all currently queued tasks and wait for any currently executed tasks to finish. Any tasks added to the BGThread after calling this function will be immediately cancelled and never executed.

unsigned Radiant::BGThread::taskCount ( )

Returns the number of tasks in the BGThread.

This includes queued tasks and currently running tasks.

Returns
Number of tasks.