RobWorkProject
Classes | Namespaces
ThreadTask.hpp File Reference

A task that facilitates the use of a hierarchic tree of tasks and subtasks. More...

#include "Ptr.hpp"
#include <boost/function.hpp>
#include <boost/thread/mutex.hpp>
#include <vector>
#include <list>

Classes

class  ThreadSafeVariable< T >
 A thread safe protected variable. More...
 
class  ThreadTask
 A task that facilitates the use of a hierarchic tree of tasks and subtasks. More...
 

Namespaces

 rw
 RobWork library.
 
 rw::common
 Various utilities and definitions of general use.
 

Detailed Description

A task that facilitates the use of a hierarchic tree of tasks and subtasks.

Often parallel processing can be done at multiple levels. Typically it is not known beforehand if some task can be split into multiple smaller subtasks or not. The ThreadTask keeps track of the state of all its subtasks - only when all subtasks have been processed, the parent task will be able to finish. Instead of finishing, it can also choose to add new subtasks that depends on the result of the previously run subtasks.

The ThreadTask can utilize a ThreadPool of arbitrary size (down to 0 threads). When 0 threads are used, the addSubTask() function will be blocking and execute the work immediately. If more than 0 threads are used, the addSubTask function will return immediately, and the task is instead added to the work queue for processing when a thread becomes available.

There are two ways to use the ThreadTask:

The four standard functions are as follows:

run() is the main work unit of the ThreadTask.

subTaskDone() is called each time a subtask has ended.

idle() is called when the task has finished its run() function and all subtasks has ended.

done() is the final function called before the ThreadTask is ended completely.

Please remember that the four functions can in general not be expected to be run in the same thread! In the first three functions it is allowed to add new subtasks to keep the thread running. In the done() function this is not allowed (it is simply ignored as the task will end immediately after this function has been called).