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

结果图:

发现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 ;
}

结果为

其实这里遇到两个小问题,第一个就是作为参数传入的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吧

CAF(C++ actor framework)使用随笔(send sync_send)(二)的更多相关文章

  1. CAF(C++ actor framework)使用随笔(unbecome与keep_behavior用法)

    看usermanual(使用随笔一里面有)看到差不多一半的时候,这个keep_behavior与unbeacome的结合引起了我的注意.(这是为什么呢?) 因为它的示例代码写的太简单了!我真的没看太懂 ...

  2. CAF(C++ actor framework)使用随笔(使用类去构建actor和使用的一些思路)

    Class-based actorsA class-based actor is a subtype of event_based_actor and must implement the pure ...

  3. CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)

    e). 消息延迟发送(和前面没太大区别直接上代码) #include <iostream> #include "caf/all.hpp" #include " ...

  4. CAF(C++ actor framework)使用随笔(projection 用法)(一)

    最近干活在写毕设,用到了CAF,看了文档,发现了一些小坑,自己摸索写点随笔.(CAF的github网站 https://github.com/actor-framework/actor-framewo ...

  5. CAF(C++ actor framework)使用随笔(同步发送 异步与同步等待)(三)

    c). 同步发送, 等待响应, 超时后收到1个系统消息. 贴上代码 #include <iostream> #include "caf/all.hpp" #includ ...

  6. CAF(C++ actor framework)(序列化之结构体,任意嵌套STL)(一)

    User-Defined Data Types in Messages(用户自定义类型)All user-defined types must be explicitly “announced” so ...

  7. CAF(C++ actor framework)(序列化之类,无需序列化,直接传)(二)

    昨天讲了Struct,还是不够满意,毕竟C++里面类用的比较多嘛,那就先上个类, 这段代码是我稍微改编了一下的结果.都是最基本的用法. #include <utility> #includ ...

  8. CAF(C++ actor framework)(序列化之复杂类,分析 还有自己不懂的细思恐极函数实现)(三)

    这里应该是序列化的最后一篇.感觉自己写的不是很好,也一点点在学习.这次就不贴上代码了.代码在github上的announce5.cpp.代码简单,但是分析下去会有细思恐极的感觉! 先看一下几个函数是干 ...

  9. “幕后英雄”之Backing Fields【Microsoft Entity Framework Core随笔】

    刘德华 有一首歌叫<马桶>,其中有一句歌词是:每一个马桶都是英雄. EFCore也有一个英雄,在幕后默默地任劳任怨.它就叫 "支持字段" (Backing Fields ...

随机推荐

  1. Android自定义下拉刷新

    网上的下拉刷新功能很多,不过基本上都是隐藏header的,而项目里面需要只隐藏部分的header,类似QQ好友动态的效果,修改了一些现有的,最后有很多问题,所以就自己自定义了一个,逻辑也很简单,首先就 ...

  2. [原创]Android秒杀倒计时自定义TextView

    自定义TextView控件TimeTextView代码: import android.content.Context; import android.content.res.TypedArray; ...

  3. 已知有一个Worker 类如下:  public class Worker  { private int age;  private String name;  private double salary;  public Worker (){}  public Worker (String nam

    package homework006; public class Worker { private int age; private String name; private double sala ...

  4. 【Cocos2d-X开发学习笔记】第09期:渲染框架之菜单类(CCMenu)的使用

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010    一.菜单项(CCMenuItem) 菜单项 ...

  5. JS 数据类型转换-转换函数、强制类型转换、利用js变量弱类型转换

    1. 转换函数: js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把值转换成浮点数.只有对String类型调用这些方法,这两个函数才能正确运行:对其他类型 ...

  6. java堆栈 (转)

     Java栈与堆 ----对这两个概念的不明好久,最终找到一篇好文,拿来共享 1. 栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自己主动管理栈和堆, ...

  7. c++简单的ATL COM开发和调用实例(转)

    c++简单的ATL COM开发和调用实例 1.打开VS2010,新建ATL COM 项目,步骤:“文件” -->“新建” -->“项目”,选择“Visual C++” -->“ATL ...

  8. DebuggingWithGdb

    https://wiki.python.org/moin/DebuggingWithGdb http://blog.nsfocus.net/python-program-troubleshooting ...

  9. linux高级命令组合

    ps -auxww | grep httpd 快速找到正在运行的apache服务安装目录 find / -path  'sina_app_v3*' 快速找到根目录下面的sina_app_v3目录 fi ...

  10. C#_delegate - 异步调用实例 BeginInvoke EndInvoke event

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...