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 ;
}
结果图:
发现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)(二)的更多相关文章
- CAF(C++ actor framework)使用随笔(unbecome与keep_behavior用法)
看usermanual(使用随笔一里面有)看到差不多一半的时候,这个keep_behavior与unbeacome的结合引起了我的注意.(这是为什么呢?) 因为它的示例代码写的太简单了!我真的没看太懂 ...
- CAF(C++ actor framework)使用随笔(使用类去构建actor和使用的一些思路)
Class-based actorsA class-based actor is a subtype of event_based_actor and must implement the pure ...
- CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)
e). 消息延迟发送(和前面没太大区别直接上代码) #include <iostream> #include "caf/all.hpp" #include " ...
- CAF(C++ actor framework)使用随笔(projection 用法)(一)
最近干活在写毕设,用到了CAF,看了文档,发现了一些小坑,自己摸索写点随笔.(CAF的github网站 https://github.com/actor-framework/actor-framewo ...
- CAF(C++ actor framework)使用随笔(同步发送 异步与同步等待)(三)
c). 同步发送, 等待响应, 超时后收到1个系统消息. 贴上代码 #include <iostream> #include "caf/all.hpp" #includ ...
- CAF(C++ actor framework)(序列化之结构体,任意嵌套STL)(一)
User-Defined Data Types in Messages(用户自定义类型)All user-defined types must be explicitly “announced” so ...
- CAF(C++ actor framework)(序列化之类,无需序列化,直接传)(二)
昨天讲了Struct,还是不够满意,毕竟C++里面类用的比较多嘛,那就先上个类, 这段代码是我稍微改编了一下的结果.都是最基本的用法. #include <utility> #includ ...
- CAF(C++ actor framework)(序列化之复杂类,分析 还有自己不懂的细思恐极函数实现)(三)
这里应该是序列化的最后一篇.感觉自己写的不是很好,也一点点在学习.这次就不贴上代码了.代码在github上的announce5.cpp.代码简单,但是分析下去会有细思恐极的感觉! 先看一下几个函数是干 ...
- “幕后英雄”之Backing Fields【Microsoft Entity Framework Core随笔】
刘德华 有一首歌叫<马桶>,其中有一句歌词是:每一个马桶都是英雄. EFCore也有一个英雄,在幕后默默地任劳任怨.它就叫 "支持字段" (Backing Fields ...
随机推荐
- VMware 虚拟机安装 Mac OS X Mountain Lion 苹果系统
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 【转】二叉树 VS hashtable
hash_table和二叉搜索树都经常被用来构建符号表(或者字典)以及相关的结构,并且他们都表现出了很高的效率.最近也在不同的程序中使用了这两种数据结构,实现完毕后思考一下,对两者做了一个简单的比较: ...
- Indesign多媒体富交互插件【MagBuilder】与iOS app 【MagViewer】介绍
[写在前面]进园子有一段时间了,从来都是看别人的文章,自己的一点东西都记在本地笔记里,现在想把一些东西拿来出分享,希望能够认识一些志同道合的朋友和老师. 学习Adobe插件开发的初衷是为了给PS做插件 ...
- 块设备驱动之NAND FLASH驱动程序
转载请注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/25240909 一.框架总结 watermark/2/text/aHR0cDov ...
- hibernate 使用C3P0数据源
1.导入jar包: hibernate-release-4.3.5.Final/lib/optional/*.jar 2.增加配置: <!-- 配置 C3P0 数据源 --> <pr ...
- redis实现spring-redis-data的入门实例
redis的客户端实现.主要分为spring-redis-data .jredis. 记录下spring-redis-data的学习心得:spring-redis-data 中我目前主要用了它的存.取 ...
- 10个你可能从未用过的PHP函数(转)
1. sys_getloadavg() sys_getloadavt()可以获得系统负载情况.该函数返回一个包含三个元素的数组,每个元素分别代表系统再过去的1.5和15分钟内的平均负载. 与其让服务器 ...
- .NET下解析Json的方法
.NET下几种常见的解析JSON方法 主要类 命名空间 限制 内建LINQ支持 DataContractJsonSerializer System.Runtime.Serialization.Json ...
- 将项目添加到Finder侧边栏和工具栏
转: http://www.cnblogs.com/wormday/archive/2011/05/08/2039468.html 1.在侧边栏和工具栏右键,有相应的设置选项 2.可以将项目拖到侧边栏 ...
- Overview & Change Log
Overview & Change Log Nova Framework is a PHP 5.5+ MVC Framework. It's designed to be lightweigh ...