c/c++函数中断 伪线程 (while 延迟)

程序主流程依靠事件循环,又有异步的需求,使用线程管理又面临锁同步问题,不想用线程来破坏设计,可以这么设计执行流程,通过责任链设计模式,设定一个执行流程

用处:延迟处理,比如延时计算中

设计两个执行函数,同时执行,在一个线程中执行结果如下:

hello world
1 d
      h 2
          3 大
1 a
      e 2
          3 德
1 d
      l 2
          3 哥
1 e
      l 2
          3 牛
1 g
      o 2
          3 逼
1 e
      o 2
1 n
      o 2
1 b
      o 2
      o 2
      o 2
      w 2
      o 2
      r 2
      r 2
      r 2
      r 2
      l 2
      l 2
      l 2
      d 2

g++ -o test main.cpp

// main.cpp
#include <string>
#include <stdio.h>
#include <functional>
#include <list>
int test();
int main(int argc, char ** argv) { return test(); }
#define LOGPOINT(x) printf("%s\n", x)
const unsigned LAMBDALOOP = 1;
const unsigned LAMBDALEAVE = 2;
const unsigned LAMBDARETURN = 0;
bool runLambdaChain(std::list<std::function<int(void *)> > &funcs, void*data);
#define L_LEAVE return LAMBDALEAVE
#define L_LOOP return LAMBDALOOP
#define L_RETURN return LAMBDARETURN
#define L_START(x) [=](void*_v) { auto v = format(_v); x;
bool runLambdaChain(std::list<std::function<int(void *)> > &funcs, void*data)
{
    if (!funcs.empty() && data)
    {
        auto func = funcs.front();
        switch(func(data)) {
        case LAMBDALOOP: break;
        case LAMBDALEAVE: funcs.pop_front(); break;
        case LAMBDARETURN: funcs.clear(); break;
        }
    }
    return !funcs.empty();
}

class Test
{
public:
    Test(){};
    int exec() {
        runLambdaChain(funcs1, data1);
        runLambdaChain(funcs2, data2);
        runLambdaChain(funcs3, data3);
        if (funcs1.size() + funcs2.size() + funcs3.size())
            return 0;
        else
            return 1;
    };

    bool Funcs3(std::list<std::function<int(void*)> > &_funcs, void *&info)
    {
        if (!_funcs.empty())
            return false;
        struct Info{
            ~Info() {};
            int i = 0;
        };
        if (info) { delete &info; info = nullptr; } info = new Info;
        static auto format = [=](void* data)->Info*{ return (Info*)data; };
        static std::list<std::function<int(void*)> > funcs = {
                [=](void*_v) { auto v = format(_v);
            LOGPOINT("          3 大");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("          3 德");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("          3 哥");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("          3 牛");
            L_LEAVE;
        },
        [=](void*_v) { auto var = format(_v);
            LOGPOINT("          3 逼");
            L_LEAVE;
        },
    };
        _funcs = funcs;
        return true;
    };

    bool Funcs1(std::list<std::function<int(void*)> > &_funcs, void *&info)
    {
        if (!_funcs.empty())
            return false;
        struct Info{
            ~Info() {};
            int i = 0;
        };
        if (info) { delete &info; info = nullptr; } info = new Info;
        static auto format = [=](void* data)->Info*{ return (Info*)data; };
        static std::list<std::function<int(void*)> > funcs = {
                [=](void*_v) { auto v = format(_v);
            LOGPOINT("1 d");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("1 a");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("1 d");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("1 e");
            L_LEAVE;
        },
        [=](void*_v) { auto var = format(_v);
            LOGPOINT("1 g");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("1 e");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("1 n");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("1 b");
            L_LEAVE;
        },
    };
        _funcs = funcs;
        return true;
    };

    bool Funcs2(std::list<std::function<int(void*)> > &_funcs, void *&info)
    {
        if (!_funcs.empty())
            return false;
        struct Info{
            ~Info() {};
            int i = 0;

        };
        if (info) { delete &info; info = nullptr; } info = new Info;
        static auto format = [=](void* data)->Info*{ return (Info*)data; };
        static std::list<std::function<int(void*)> > funcs = {
                [=](void*_v) { auto v = format(_v);
            LOGPOINT("      h 2");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      e 2");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      l 2");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      l 2");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      o 2");
            if (v->i++ < 5)
                L_LOOP;
            else
                L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      w 2");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      o 2");
            L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      r 2");
            if (v->i-- > 3)
                L_LOOP;
            else
                L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      l 2");
            if (v->i-- > 0)
                L_LOOP;
            else
                L_LEAVE;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      d 2");
            L_RETURN;
        },
        [=](void*_v) { auto v = format(_v);
            LOGPOINT("      ! 2");
            L_LEAVE;
        },
    };
        _funcs = funcs;
        return true;
    };
    void* data1 = nullptr;
    void* data2 = nullptr;
    void* data3 = nullptr;
    std::list<std::function<int (void*)>> funcs1;
    std::list<std::function<int (void*)>> funcs2;
    std::list<std::function<int (void*)>> funcs3;
};

int test()
{
    LOGPOINT("hello world");
    Test test;
    test.Funcs1(test.funcs1, test.data1);
    test.Funcs2(test.funcs2, test.data2);
    test.Funcs3(test.funcs3, test.data3);
    while (test.exec() == 0);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值