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 ...
随机推荐
- Mysql 存储过程、函数、触发器和视图的权限检查
当存储过程.函数.触发器和视图创建后,不单单创建者要执行,其它用户也可能需要执行,换句话说,执行者有可能不是创建者本身,那么在执行存储过程时,MySQL是如何做权限检查的? 在默认情况下,MySQL将 ...
- android常见错误-
将library中的报错项删除,然后点击[add]正确的appcompat
- 主流数据库字段类型转.Net类型的方法
最近在阅读一些开源的代码,发现其中有些方法总结的很全面,至少在我做同样的事情时候,需要抓破脑袋想活着google,现在看到了这个关于主流数据库字段类型转.Net类型的方法,故收藏之,也顺便分享给那些能 ...
- ShowcaseView-master
ShowcaseView.rar
- valgrind 生成mysqld调用图之 select now()跟踪
1.mysqld起动方式: 1.mysqld以root用户运行 valgrind --tool=callgrind --separate-threads=yes --trace-children=y ...
- Java,javascript,html,css的关系
内容:,就是制作者放在页面想让访问者浏览的内容 . 主要由java语言提供数据检索.更新.业务处理等. 结构:使内容更加具有逻辑性和易用性,类似于1,2级标题,正文.列表等等. HTML 表现:用于修 ...
- NPOI导出多表头Execl(通过html表格遍历表头)
关于NPOI的相关信息,我想博客园已经有很多了,而且NPOI导出Execl的文章和例子也很多,但导出多表头缺蛮少的:今天要讲的通过自己画html表格:通过html表格来导出自定义的多表头: 先来看要实 ...
- mod_rewrite模块详解
mod_rewrite模块提供了一个基于规则的(使用正则表达式分析器的)实时转向URL请求的引擎. 支持每个规则可以拥有不限数量的规则以及附加条件规则的灵活而且强大的URL操作机制. 此URL操作可以 ...
- 关于Git中的一些常用的命令
深入了解git的checkout命令 检出命令(git checkout)是Git最常用的命令之一,同时也是一个很危险的命令. 因为这条命令会重写工作区.检出命令的用法如下: 用法一: git che ...
- dig命令浅析
dig命令,功能更强大的命令. man dig dig [@server] [-b address] [-c class] [-f filename] [-k filename] [-m] \ [-p ...