| 12 | using namespace CGraph; |
| 13 | |
| 14 | void tutorial_coordinator() { |
| 15 | GPipelinePtr pipeline = GPipelineFactory::create(); |
| 16 | GElementPtr a, b, c, d, f, g, h = nullptr; |
| 17 | GCoordinatorPtr<-2> e_coordinator = nullptr; // 申明一个减少2个(-2个)辅助线程的 GCoordinator |
| 18 | |
| 19 | UThreadPoolConfig config; |
| 20 | config.default_thread_size_ = 0; |
| 21 | config.secondary_thread_size_ = 4; // 设置刚开始的时候,4个辅助线程执行 |
| 22 | pipeline->setUniqueThreadPoolConfig(config); |
| 23 | |
| 24 | CStatus status = pipeline->registerGElement<MyNode1>(&a, {}, "nodeA"); |
| 25 | status += pipeline->registerGElement<MyNode1>(&b, {}, "nodeB"); |
| 26 | status += pipeline->registerGElement<MyNode1>(&c, {}, "nodeC"); |
| 27 | status += pipeline->registerGElement<MyNode1>(&d, {}, "nodeD"); |
| 28 | /** |
| 29 | * [a,b,c,d] 并发执行结束之后,加入 GCoordinator 逻辑,减少进程内的 2 个辅助线程 |
| 30 | * 使得后面的 f->[g,h] 的逻辑,执行的时候,减少线程切换逻辑 |
| 31 | * 从而在一定程度上,提升执行效率 |
| 32 | * 注意:推荐在非并发执行的位置使用 GCoordinator 功能。使用前,请预估好线程数 |
| 33 | */ |
| 34 | status += pipeline->registerGElement<GCoordinator<-2>>(&e_coordinator, {a, b, c, d}, "coordinatorE"); |
| 35 | status += pipeline->registerGElement<MyNode1>(&f, {e_coordinator}, "nodeF"); |
| 36 | status += pipeline->registerGElement<MyNode2>(&g, {f}, "nodeG"); |
| 37 | status += pipeline->registerGElement<MyNode2>(&h, {f}, "nodeH"); |
| 38 | |
| 39 | pipeline->process(); |
| 40 | GPipelineFactory::remove(pipeline); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | int main() { |
no test coverage detected