Giter VIP home page Giter VIP logo

Comments (5)

ChunelFeng avatar ChunelFeng commented on May 14, 2024

您好,感谢您的关注。

如果考虑使用全局变量的话,推荐通过添加切面的形式,实现上述逻辑。

参考 https://github.com/ChunelFeng/CGraph/blob/main/tutorial/T09-Aspect.cpp 这个demo,以获取更多的切面的好玩的用法

bool g_bRunning = true;

class MyStopAspect : public GAspect {
public:
    CStatus beginRun() override {
        CStatus status;
        if (!g_bRunning) {
            status.setStatus("stop");   // 遇到异常状况,设置异常返回信息
        }
        return status;
    }
};


void demo() {
    GPipelinePtr pipeline = GPipelineFactory::create();
    GElementPtr a, b, c, d = nullptr;
    CStatus status;

    CStatus status = pipeline->registerGElement<MyNode1>(&a, {}, "nodeA");
    status += pipeline->registerGElement<MyNode2>(&b, {a}, "nodeB");
    status += pipeline->registerGElement<MyNode1>(&c, {a}, "nodeC");
    status += pipeline->registerGElement<MyNode2>(&d, {b, c}, "nodeD");
    pipeline->addGAspect<MyStopAspect>({a,b,c,d});    // 给每个算子,加上切面,确保在g_bRunning = false的时候,不继续执行

    pipeline->process();
    GPipelineFactory::remove(pipeline);
}

from cgraph.

libaineu2004 avatar libaineu2004 commented on May 14, 2024

这样以切面的方式停止,会不会造成资源泄漏啥的?

from cgraph.

ChunelFeng avatar ChunelFeng commented on May 14, 2024

您好。如果您的所有资源,是在 init的时候申请,destroy的时候释放,是不会造成内存泄漏的。

原因是,你加入这个 aspect的时候,实际运行逻辑,是在 bool被赋值后,执行的所有node的 run()方法 被执行前,直接就返回了。

不涉及任何资源分配,或者临时中断的内容

from cgraph.

libaineu2004 avatar libaineu2004 commented on May 14, 2024

就是说beginRun()会在node的init函数之前执行?

from cgraph.

ChunelFeng avatar ChunelFeng commented on May 14, 2024

在beginRun(), node的 run之前,见名知意。
同时,还是 beforeInit() 切面,在 init()之前。

pipeline中,所有node的 init方法和 destroy方法,均只执行一次。
run方法,可以反复多次执行。

from cgraph.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.