1 Star 0 Fork 0

tom/cpp-taskflow

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
condition.cpp 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
tsung-wei-huang 提交于 2020-01-13 13:48 +08:00 . updated benchmark page
// The example creates the following cyclic graph:
//
// A
// |
// v
// B<---|
// | |
// v |
// C----|
// |
// v
// D
//
// - A is a task that initializes a counter to zero
// - B is a task that increments the counter
// - C is a condition task that loops around B until the counter
// reaches a breaking number
// - D is a task that wraps up the result
#include <taskflow/taskflow.hpp>
int main() {
tf::Executor executor;
tf::Taskflow taskflow("Conditional Tasking Demo");
int counter;
auto A = taskflow.emplace([&](){
std::cout << "initializes the counter to zero\n";
counter = 0;
}).name("A");
auto B = taskflow.emplace([&](){
std::cout << "loops to increment the counter\n";
counter++;
}).name("B");
auto C = taskflow.emplace([&](){
std::cout << "counter is " << counter << " -> ";
if(counter != 5) {
std::cout << "loops again (goes to B)\n";
return 0;
}
std::cout << "breaks the loop (goes to D)\n";
return 1;
}).name("C");
auto D = taskflow.emplace([&](){
std::cout << "done with counter equal to " << counter << '\n';
}).name("D");
A.precede(B);
B.precede(C);
C.precede(B);
C.precede(D);
// visualizes the taskflow
taskflow.dump(std::cout);
// executes the taskflow
executor.run(taskflow).wait();
assert(counter == 5);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/tomgee/cpp-taskflow.git
git@gitee.com:tomgee/cpp-taskflow.git
tomgee
cpp-taskflow
cpp-taskflow
master

搜索帮助