| 12 | using namespace CGraph; |
| 13 | |
| 14 | void tutorial_fence() { |
| 15 | CStatus status; |
| 16 | GPipelinePtr pipeline = GPipelineFactory::create(); |
| 17 | GElementPtr b, c, e = nullptr; |
| 18 | GFunctionPtr a_function = nullptr; |
| 19 | GFencePtr d_fence = nullptr; // 通过 fence,去等待前方指定的异步节点执行完成的逻辑 |
| 20 | |
| 21 | status += pipeline->registerGElement<GFunction>(&a_function, {}, "functionA"); |
| 22 | status += pipeline->registerGElement<MyNode2>(&b, {a_function}, "nodeB"); |
| 23 | status += pipeline->registerGElement<MyNode1>(&c, {a_function}, "nodeC"); |
| 24 | status += pipeline->registerGElement<GFence>(&d_fence, {b, c}, "fenceD"); |
| 25 | status += pipeline->registerGElement<MyNode1>(&e, {d_fence}, "nodeE"); |
| 26 | |
| 27 | CSec sec = 5; |
| 28 | a_function->setFunction(CFunctionType::RUN, [a_function, sec] { |
| 29 | CGRAPH_ECHO("[%s] begin sleep for [%ld]s", a_function->getName().c_str(), sec); |
| 30 | CGRAPH_SLEEP_SECOND(sec) |
| 31 | CGRAPH_ECHO("[%s] run finished.", a_function->getName().c_str()); |
| 32 | return CStatus(); |
| 33 | }); |
| 34 | |
| 35 | // 设定一个执行 5s的功能,并且让其运行200就进入超时,故后面的 b和c,可以运行起来了 |
| 36 | a_function->setTimeout(200, GElementTimeoutStrategy::HOLD_BY_PIPELINE); |
| 37 | b->setTimeout(200, GElementTimeoutStrategy::HOLD_BY_PIPELINE); |
| 38 | |
| 39 | /** |
| 40 | * 在这里添加一个栅栏,目的是为了等待 a_function, b 都执行完成之后,后面的逻辑才可以开始 |
| 41 | * fence中加入的,必须是异步执行(设置 timeout耗时)的element |
| 42 | * 设定为 HOLD_BY_PIPELINE 的element,如果不加入 fence中,则在 pipeline run结束之前,会等待其执行结束 |
| 43 | */ |
| 44 | d_fence->waitGElements({a_function, b}); |
| 45 | |
| 46 | status = pipeline->process(); |
| 47 | if (status.isErr()) { |
| 48 | std::cout << status.getInfo() << std::endl; |
| 49 | } |
| 50 | |
| 51 | GPipelineFactory::remove(pipeline); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | int main () { |
no test coverage detected