| Taskflow: A General-purpose Task-parallel Programming System

Loading...
Searching...
No Matches
tf::RuntimeTaskLike Concept Reference

determines if a callable is a runtime task More...

`#include <taskflow/core/task.hpp>`

## Concept definition

template<typename C>
concept tf::RuntimeTaskLike =
(std::invocable<C, tf::Runtime&> &&
std::same_as<std::invoke_result_t<C, tf::Runtime&>, void>) ||
(std::invocable<C, tf::NonpreemptiveRuntime&> &&
std::same_as<std::invoke_result_t<C, tf::NonpreemptiveRuntime&>, void>)
tf::RuntimeTaskLike
determines if a callable is a runtime task
Definition task.hpp:303

## Detailed Description

determines if a callable is a runtime task

A runtime task is a callable object that accepts either tf::Runtime& or tf::NonpreemptiveRuntime& and returns void. Runtime tasks provide access to the executor's runtime information, allowing tasks to spawn work at runtime, query the running state, and control execution flow.

Requirements
  - Must be invocable with either tf::Runtime& OR tf::NonpreemptiveRuntime&
  - Must return void

Examples

// Valid runtime tasks (satisfy RuntimeTaskLike)
// Runtime task with tf::Runtime
auto runtime_task1 = [](tf::Runtime& rt) {
std::cout << "running with " << rt.num_workers() << " workers\n";
};
static_assert(RuntimeTaskLike<decltype(runtime_task1)>);
// Runtime task with tf::NonpreemptiveRuntime
auto runtime_task2 = [](tf::NonpreemptiveRuntime& rt) {
std::cout << "non-preemptive runtime\n";
};
static_assert(RuntimeTaskLike<decltype(runtime_task2)>);
// Runtime task spawning nested work
auto spawn_work = [](tf::Runtime& rt) {
// Can query and control execution at runtime
rt.spawn([](){ std::cout << "spawned task\n"; });
};
static_assert(RuntimeTaskLike<decltype(spawn_work)>);
// Function object
struct RuntimeTask {
void operator()(tf::Runtime& rt) const {
std::cout << "runtime task\n";
}
};
static_assert(RuntimeTaskLike<RuntimeTask>);
// Use in taskflow
tf::Taskflow taskflow;
auto task = taskflow.emplace(runtime_task1).name("runtime");
tf::FlowBuilder::emplace
Task emplace(C &&callable)
creates a static task
Definition flow_builder.hpp:1738
tf::Runtime
class to create a runtime task
Definition runtime.hpp:47
tf::Task::name
const std::string & name() const
queries the name of the task
Definition task.hpp:1388
tf::Taskflow
class to create a taskflow object
Definition taskflow.hpp:64

Invalid Examples

// Invalid: no parameters
auto not_runtime1 = []() { std::cout << "no params\n"; };
// static_assert(RuntimeTaskLike<decltype(not_runtime1)>); // FAILS
// Invalid: wrong parameter type
auto not_runtime2 = [](int x) { std::cout << x << '\n'; };
// static_assert(RuntimeTaskLike<decltype(not_runtime2)>); // FAILS
// Invalid: returns non-void
auto not_runtime3 = [](tf::Runtime& rt) { return 42; };
// static_assert(RuntimeTaskLike<decltype(not_runtime3)>); // FAILS