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吧

最新文章

  1. 【Telnet】使用Telnet协议连接到远程Shell执行脚本
  2. buffer和cache有什么本质区别
  3. 星外虚拟主机跨web目录文件读取漏洞
  4. Redis服务器配置
  5. I.MX6 Android netperf
  6. 人生规划和GTD——“知”、“得”与“合”
  7. Spring和Hibernate相遇
  8. 简单的API应用
  9. HDU 4391 - Paint The Wall - 分块哈希入门
  10. 超酷创意HTML5动画演示及代码
  11. UNIX时间与本地时间的转换
  12. wordpress建站过程5——footer.php
  13. 数组中的reduce 函数理解
  14. mysql数据库 ,java 代码巧妙结合提升系统性能。
  15. Spring-1-(简介及HelloWord)
  16. CentOS_mini下make安装
  17. js阻止默认事件,如a标签跳转和事件冒泡
  18. Unity shader学习之切线空间下计算凹凸映射
  19. kbmMW 5.07.00试用笔记
  20. jQuery 闪动的文字提示

热门文章

  1. 如何创建一个有System用户权限的命令行
  2. postfix 不记录日志的问题解决方法
  3. cocos2d-x sprite触摸处理
  4. SAP增强总结-第二代增强(SMOD、CMOD)【转载】
  5. C# 创建、安装和卸载Windows服务程序
  6. android---APN切换
  7. unity3d NGUI多场景共用界面制作
  8. [ZZ]良好的编码习惯
  9. javaScript面向对象基础
  10. Android_UI