程序主流程依靠事件循环,又有异步的需求,使用线程管理又面临锁同步问题,不想用线程来破坏设计,可以这么设计执行流程,通过责任链设计模式,设定一个执行流程
用处:延迟处理,比如延时计算中
设计两个执行函数,同时执行,在一个线程中执行结果如下:
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;
}