CAF(C++ actor framework)使用随笔(send sync_send)(二)

a). 发完就忘, 就像上面anon_send 以及send

#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include <string>
using namespace std;
using namespace caf;


behavior fun(event_based_actor* self){
    return {
        [self](const string& str,actor a) {
            aout(self)<<"get message: "<<str<<endl;
            //get the actor a's address
            aout(self)<<to_string(a.address())<<endl;
            // send message anonymously
            anon_send(a,"hello");
            // get lastest current message
            aout(self)<<"current_message: "<<to_string(self->current_message())<<endl;
            aout(self)<<"current_sender: "<<to_string(self->current_sender())<<endl;
            self->quit();
            return;
         }
        };
}

int main(){
    auto actor1 = spawn(fun);
     {
    scoped_actor self;
    self->send(actor1,"anon_send",self);
    //wrong code  scoped_actor do not have anon_send and become function

    // self->become(
    //     [=](const string& str){
    //         aout(self)<<"get message: "<<str<<endl;
    //     );
    }
    caf::await_all_actors_done();
    shutdown();
    return 0;
}

结果图:

CAF(C++ actor framework)使用随笔(send sync_send)(二)

发现scoped_actor 不能使用anon_send 和becoem函数,但我个人理解scoped的send就是anon的发送,发完他就消失了。

b).同步发送, 等待响应. 等待是异步的, 相当于只是期待1个响应.

贴上代码

#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include <string>
using namespace std;
using namespace caf;


behavior fun(event_based_actor* self){
    return {
        [self](const string& str) {
            self->quit();
            return "hello, world"; 
         }
    };
}

void fun1(event_based_actor* self, const actor &buddy){
    self->sync_send(buddy,"hi!").then(
        [=](const string& str) {
        aout(self)<<str<<endl;
        }
    );
    aout(self)<<"i'm not waiting for you!"<<endl;
}

int main(){
    auto actor1 = spawn(fun);
    auto actor2 = spawn(fun1,actor1);
    caf::await_all_actors_done();
    shutdown();
    return 0;
}

结果为

CAF(C++ actor framework)使用随笔(send sync_send)(二)

其实这里遇到两个小问题,第一个就是作为参数传入的actor buddy必须定义为const,不然编译会报错,第二个问题,我自己认为这个代码的结果应该会需要我ctrl+C去结束。结果,我发现actor2在接受到这个消息之后跑完也就调用quit()了,那么我自己就把then()里面的代码注释起来,再编译通过了,运行的时候就coredump了。想到了c++11里面 如果线程不join也会发生coredump。所以还是要注意这个问题,目前还没讲异常处理。

--------------------------------------------------3.15 更新线-----------------------------------------------------------

发现一个bug 就是两个进程通讯时(使用remoter actor的情况下!),假设又两个进程A,B 当A 使用send 发给B 一个message 的时候,B会收到这个message,但是不会收到这个传过来的message内容,而使用sync_send 不管是await也好then 也好都是可以让B收到消息的内容的。那么第二次再send 或者sync_send时 却都是一样的。那我一开始理解为publish 一个端口时会需要花费一些时间,所以send发过来没收到,但是sync_send却可以,所以我认为应该是一个bug吧
原文链接: https://www.cnblogs.com/zhejiangxiaomai/p/5242474.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/229573

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午2:22
下一篇 2023年2月13日 下午2:22

相关推荐