zoukankan      html  css  js  c++  java
  • CAF(C++ actor framework)使用随笔(unbecome与keep_behavior用法)

    看usermanual(使用随笔一里面有)看到差不多一半的时候,这个keep_behavior与unbeacome的结合引起了我的注意。(这是为什么呢?)

      因为它的示例代码写的太简单了!我真的没看太懂,我就自己把他的改了改放上来。

    先讲一下,基本概念,就是一个actor可以有多个行为(behavior)那么become就可以让一个actor变成一种行为。

    如果使用了keep_behavior呢就会把当前的行为压入“行为栈”(behavior stack),  调用unbecome就可以变成行为栈上最前面的一个了。

    比如我先在行为A的时候keep_behavior,后来我再在行为B的时候keep_behavior一下,那么我此时调用unbecome的时候会变成哪个种行为呢?答案当然是行为B咯!

    贴上代码

    #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;
    }

    结果为

    如果把value2循环中的keep_behavior去掉结果就是

    之后开始准备写caf序列化方面。真的是挺好用,挺炫酷。

    最后弱弱的说一句,求互粉阿!

  • 相关阅读:
    对焦过程中消除摩尔纹
    Python3.x:Linux下安装python3.6
    Python3.x:Linux下退出python命令行
    Python3.x:ConfigParser模块的使用
    Python3.x:SQLAlchemy操作数据库
    Python3.x:遍历select下拉框获取value值
    Python3.x:Selenium中的webdriver进行页面元素定位
    Python3.x:selenium获取iframe内嵌页面的源码
    Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页
    Python3.x:将数据下载到xls时候用xml格式保存一份读取内容
  • 原文地址:https://www.cnblogs.com/zhejiangxiaomai/p/5257937.html
Copyright © 2011-2022 走看看