1 Star 0 Fork 37

uxsys/cpp-taskflow

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
run.cpp 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
alexbriskin 提交于 2022-03-22 04:24 +08:00 . Remove trailing whitespace (#386)
// This example demonstrates how to use different methods to
// run a taskflow.
#include <taskflow/taskflow.hpp>
int main(){
// create an executor and a taskflow
tf::Executor executor(1);
tf::Taskflow taskflow("Demo");
auto A = taskflow.emplace([&](){ std::cout << "TaskA\n"; }).name("A");
auto B = taskflow.emplace([&](tf::Subflow& subflow){
std::cout << "TaskB\n";
auto B1 = subflow.emplace([&](){ std::cout << "TaskB1\n"; }).name("B1");
auto B2 = subflow.emplace([&](){ std::cout << "TaskB2\n"; }).name("B2");
auto B3 = subflow.emplace([&](){ std::cout << "TaskB3\n"; }).name("B3");
B1.precede(B3);
B2.precede(B3);
}).name("B");
auto C = taskflow.emplace([&](){ std::cout << "TaskC\n"; }).name("C");
auto D = taskflow.emplace([&](){ std::cout << "TaskD\n"; }).name("D");
A.precede(B, C);
B.precede(D);
C.precede(D);
// dumpping a taskflow before execution won't visualize subflow tasks
std::cout << "Dump the taskflow before execution:\n";
taskflow.dump(std::cout);
std::cout << "Run the taskflow once without callback\n" << std::endl;
executor.run(taskflow).get();
std::cout << std::endl;
// after execution, we can visualize subflow tasks
std::cout << "Dump the taskflow after execution:\n";
taskflow.dump(std::cout);
std::cout << std::endl;
std::cout << "Use wait_for_all to wait for the execution to finish\n";
executor.run(taskflow).get();
executor.wait_for_all();
std::cout << std::endl;
std::cout << "Execute the taskflow two times without a callback\n";
executor.run(taskflow).get();
std::cout << "Dump after two executions:\n";
taskflow.dump(std::cout);
std::cout << std::endl;
std::cout << "Execute the taskflow four times with a callback\n";
executor.run_n(taskflow, 4, [] () { std::cout << "finishes 4 runs\n"; })
.get();
std::cout << std::endl;
std::cout << "Run the taskflow until the predicate returns true\n";
executor.run_until(taskflow, [counter=3]() mutable {
std::cout << "Counter = " << counter << std::endl;
return counter -- == 0;
}).get();
taskflow.dump(std::cout);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/uxsys/cpp-taskflow.git
git@gitee.com:uxsys/cpp-taskflow.git
uxsys
cpp-taskflow
cpp-taskflow
master

搜索帮助