看usermanual(使用随笔一里面有)看到差很少一半的时候,这个keep_behavior与unbeacome的结合引发了个人注意。(这是为何呢?)ios
由于它的示例代码写的太简单了!我真的没看太懂,我就本身把他的改了改放上来。spa
先讲一下,基本概念,就是一个actor能够有多个行为(behavior)那么become就能够让一个actor变成一种行为。3d
若是使用了keep_behavior呢就会把当前的行为压入“行为栈”(behavior stack), 调用unbecome就能够变成行为栈上最前面的一个了。code
好比我先在行为A的时候keep_behavior,后来我再在行为B的时候keep_behavior一下,那么我此时调用unbecome的时候会变成哪一个种行为呢?答案固然是行为B咯!
blog
贴上代码string
#include <string> #include <iostream> #include "caf/io/all.hpp" #include "caf/all.hpp" using namespace std; using namespace caf; behavior testee(event_based_actor* self) { return { [=](int value1) { cout<<"value1:"<<value1<<endl; self->become ( keep_behavior, [=](float value2) { cout << "value2:" << value2 << endl; self->become ( keep_behavior, [=](double value3){ cout<<"value3:"<<value3<<endl; self->unbecome(); } ); }); } }; } int main(){ auto actor1 = spawn(testee); { caf::scoped_actor self; int a1 = 1; float a2 = 1.1; double a3 = 1.2; int b1 = 2; float b2 = 2.1; double b3 = 2.2; self->send(actor1,a1); self->send(actor1,a2); self->send(actor1,a3); self->send(actor1,b1); self->send(actor1,b2); self->send(actor1,b3); } caf::await_all_actors_done(); shutdown(); return 0; }
结果为it
若是把value2循环中的keep_behavior去掉结果就是io
以后开始准备写caf序列化方面。真的是挺好用,挺炫酷。event
最后弱弱的说一句,求互粉阿!class