tf::Task class | Taskflow QuickStart

# tf::Task class

class to create a task handle over a node in a taskflow graph

A task is a wrapper over a node in a taskflow graph. It provides a set of methods for users to access and modify the attributes of the associated node in the taskflow graph. A task is very lightweight object (i.e., only storing a node pointer) that can be trivially copied around, and it does not own the lifetime of the associated node.

## Constructors, destructors, conversion operators

Task() defaulted
  constructs an empty task
Task(const Task& other)
  constructs the task with the copy of the other task

## Public functions

auto operator=(const Task&) -> Task&
  replaces the contents with a copy of the other task
auto operator=(std::nullptr_t) -> Task&
  replaces the contents with a null pointer
auto operator==(const Task& rhs) const -> bool
  compares if two tasks are associated with the same graph node
auto operator!=(const Task& rhs) const -> bool
  compares if two tasks are not associated with the same graph node
auto name() const -> const std::string&
  queries the name of the task
auto num_successors() const -> size_t
  queries the number of successors of the task
auto num_dependents() const -> size_t
  queries the number of predecessors of the task
auto num_strong_dependents() const -> size_t
  queries the number of strong dependents of the task
auto num_weak_dependents() const -> size_t
  queries the number of weak dependents of the task
auto name(const std::string& name) -> Task&
  assigns a name to the task
template<typename C>
auto work(C&& callable) -> Task&
  assigns a callable
template<typename T>
auto composed_of(T& object) -> Task&
  creates a module task from a taskflow
template<typename... Ts>
auto precede(Ts && ... tasks) -> Task&
  adds precedence links from this to other tasks
template<typename... Ts>
auto succeed(Ts && ... tasks) -> Task&
  adds precedence links from other tasks to this
auto release(Semaphore& semaphore) -> Task&
  makes the task release this semaphore
auto acquire(Semaphore& semaphore) -> Task&
  makes the task acquire this semaphore
auto data(void* data) -> Task&
  assigns pointer to user data
auto priority(TaskPriority p) -> Task&
  assigns a priority value to the task
auto priority() const -> TaskPriority
  queries the priority value of the task
void reset()
  resets the task handle to null
void reset_work()
  resets the associated work to a placeholder
auto empty() const -> bool
  queries if the task handle points to a task node
auto has_work() const -> bool
  queries if the task has a work assigned
template<typename V>
void for_each_successor(V&& visitor) const
  applies an visitor callable to each successor of the task
template<typename V>
void for_each_dependent(V&& visitor) const
  applies an visitor callable to each dependents of the task
auto hash_value() const -> size_t
  obtains a hash value of the underlying node
auto type() const -> TaskType
  returns the task type
void dump(std::ostream& ostream) const
  dumps the task through an output stream
auto data() const -> void*
  queries pointer to user data

## Function documentation

### Task& tf::Task::name(const std::string& name)

assigns a name to the task

Parameters
name | a std::string acceptable string
Returns | `*this`

###
template<typename C>
Task& tf::Task::work(C&& callable)

assigns a callable

Template parameters
C | callable type
Parameters
callable | callable to construct a task
Returns | `*this`

###
template<typename T>
Task& tf::Task::composed_of(T& object)

creates a module task from a taskflow

Template parameters
T | object type
Parameters
object | a custom object that defines `T::graph()` method
Returns | `*this`

###
template<typename... Ts>
Task& tf::Task::precede(Ts && ... tasks)

adds precedence links from this to other tasks

Template parameters
Ts | parameter pack
Parameters
tasks | one or multiple tasks
Returns | `*this`

###
template<typename... Ts>
Task& tf::Task::succeed(Ts && ... tasks)

adds precedence links from other tasks to this

Template parameters
Ts | parameter pack
Parameters
tasks | one or multiple tasks
Returns | `*this`

### Task& tf::Task::data(void* data)

assigns pointer to user data

Parameters
data | pointer to user data
Returns | `*this`

The following example shows how to attach user data to a task and run the task iteratively while changing the data value:

    tf::Executor executor;
    tf::Taskflow taskflow("attach data to a task");

    int data;

    // create a task and attach it the data
    auto A = taskflow.placeholder();
    A.data(&data).work([A](){
      auto d = *static_cast<int*>(A.data());
      std::cout << "data is " << d << std::endl;
    });

    // run the taskflow iteratively with changing data
    for(data = 0; data<10; data++){
      executor.run(taskflow).wait();
    }

### Task& tf::Task::priority(TaskPriority p)

assigns a priority value to the task

A priority value can be one of the following three levels, tf::TaskPriority::HIGH (numerically equivalent to 0), tf::TaskPriority::NORMAL (numerically equivalent to 1), and tf::TaskPriority::LOW (numerically equivalent to 2). The smaller the priority value, the higher the priority.

Tab / T to search, Esc to close
…

Search for symbols, directories, files, pages or modules. You can omit any prefix from the symbol or file path; adding a `:` or `/` suffix lists all members of given symbol or directory.

Use ↓ / ↑ to navigate through the list, Enter to go. Tab autocompletes common prefix, you can copy a link to the result using ⌘ L while ⌘ M produces a Markdown link.

Sorry, nothing was found.