e). 消息延迟发送(和前面没太大区别直接上代码)

#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include <string>
#include <chrono>
using namespace std;
using namespace caf; behavior fun(event_based_actor* self){
return {
[self](const string& str){
aout(self)<<str<<endl;
auto t2 = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
cout<<"dalay time :"<<t2<<endl;
self->quit();
}
};
} void fun1(event_based_actor* self, actor buddy){
self->delayed_send(buddy, std::chrono::seconds(), "hi!");
} int main(){
auto actor1 = spawn(fun);
auto t1 = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
cout<<"before delayed_send :"<<t1<<endl;
auto actor2 = spawn(fun1,actor1); caf::await_all_actors_done();
shutdown();
return ;
}

结果为

f). 消息前转(消息转发)forward.

贴上代码

#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include <string>
#include <chrono>
using namespace std;
using namespace caf; behavior fun2(event_based_actor* self){
return {
[self](const string& str){
aout(self)<<"C get message return to A"<<endl;
aout(self)<<"C's address is :"<<self->address()<<endl;
return "hello, A";
self->quit();
}
};
} behavior fun1(event_based_actor* self, const actor &buddy){
return {
[=](const string& str){
aout(self)<<"B get message forward to C"<<endl;
self->forward_to(buddy);
self->quit();
}
};
} void fun(event_based_actor* self, const actor &buddy){
self->sync_send(buddy,"hi!").then(
[=](const string& str) {
aout(self)<<str<<endl;
aout(self)<<"A think last_sender is :"<<self->last_sender()<<endl;
}
);
aout(self)<<"A send to B!"<<endl;
} int main(){
auto actorC = spawn(fun2);
auto actorB = spawn(fun1,actorC);
auto actorA = spawn(fun,actorB);
caf::await_all_actors_done();
shutdown();
return ;
}

结果为

使用了消息优先级,去caf代码里看了发现只有两种级别就是(hige 和normal)

然后在用户手册上给出的示例代码是

using a_atom = atom_constant<atom("a")>;
using b_atom = atom_constant<atom("b")>; behavior testee(event_based_actor* self) {
// send 'b' with normal priority
self->send(self, b_atom::value);
// send 'a' with high priority
self->send(message_priority::high, self, a_atom::value);
// terminate after receiving a 'b'
return {
[=](b_atom) {
aout(self) << "received 'b' => quit" << endl;
self->quit();
},
[=](a_atom) {
aout(self) << "received 'a'" << endl;
},
};
} int main() {
// will print "received 'b' => quit"
spawn(testee);
await_all_actors_done();
// will print "received 'a'" and then "received 'b' => quit"
spawn<priority_aware>(testee);
await_all_actors_done();
shutdown();
}

结果是

很好理解,因为第一个actor是不认识具有优先级的message的,只受到b消息,第二个就先收到a再收到b,这里有个细节就是当B本来因该是先收到的,但却变成了后收到,和发送的次序无关了。我甚至在两个发送语句之间加usleep(1000000) 都会先收到A后再输出B,这个Actor 感觉自己知道要收到priority 消息。

我还是不罢休,把代码改了改变成

结果为

我彻底服了CAF 太牛了把,当Actor不识别优先级时,它会把所有消息当成normal(正常的),当它知道时,就会把所有的优先级消息都接受完了再接受normal的。后来我改写了一些奇怪的语句,这CAF还是要我挺佩服得。

到这里消息都讲完了,之后开始CAF序列化之路。。。

CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)的更多相关文章

  1. Runtime-消息发送和消息转发

    消息发送 消息发送举例:下面这个OC代码 [person read:book]; 会被编译成: objc_msgSend(person, @selector(read:), book); objc_m ...

  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. Android 手机卫士--解析json与消息机制发送不同类型消息

    本文地址:http://www.cnblogs.com/wuyudong/p/5900800.html,转载请注明源地址. 1.解析json数据 解析json的代码很简单 JSONObject jso ...

  4. 【转】C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值(1)

    C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值(1) #region 截获消息        /// 截获消息  处理XP不能关机问题        protected ...

  5. SendMessage发送自定义消息及消息响应

    控件向父窗体发送自定义消息,父窗体定义处理此消息的函数   效果描述: 指定哪个类添加自定义消息:(当然这个类必须是CmdTarget的子类,不然不能处理消息) 添加消息 实现消息函数:(wParam ...

  6. iOS 消息发送与转发详解

    Objective-C 是一门动态语言,它将很多静态语言在编译和链接时期做的事情,放到了运行时来处理.之所以能具备这种特性,离不开 Runtime 这个库.Runtime 很好的解决了如何在运行时期找 ...

  7. ROS_Kinetic_26 使用rosserial_windows实现windows与ROS master发送与接收消息

    使用rosserial_windows实现windows与ROS master发送与接收消息(适用版本hydro,indigo,jade,kinetic) 官方wiki地址汇总请参考:http://b ...

  8. [3] MQTT,mosquitto,Eclipse Paho---怎样使用 Eclipse Paho MQTT工具来发送订阅MQTT消息?

    在上两节,笔者主要介绍了 MQTT,mosquitto,Eclipse Paho的基本概念已经怎样安装mosquitto. 在这个章节我们就来看看怎样用 Eclipse Paho MQTT工具来发送接 ...

  9. C++ Custom Control控件 向父窗体发送对应的消息

    向父窗体发送消息 ,这里只讲发送 WM_NOTIFY  消息, 其它消息是相同的 在 控件中的某个函数中 设置发送消息的程序 首先定义一个WM_NOTIFY消息的专用结构. NMHDR nm; nm. ...

随机推荐

  1. BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树

    2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...

  2. The Romantic Hero

    Problem Description There is an old country and the king fell in love with a devil. The devil always ...

  3. cocos2dx新建android项目lib拷贝、访问权限等问题集

    win7上面搭建cocos2dx的android项目,用create-android-project.bat新建一个项目时,出现了访问权限和项目无lib的问题. 解决方案: 1.项目无lib问题. 当 ...

  4. KindEditor编辑器, 利用ajax动态切换编辑器内容

    // 后台成功返回数据后的js处理 KindEditor.remove('#content_id'); // 先移除之前创建的编辑器 var editor = KindEditor.create('# ...

  5. Objective-C ,ios,iphone开发基础:几个常用类-NSNumber

    2013-08-21 在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objectiv ...

  6. DHCP服务详解

    DHCP概念和原理 dhcp服务作用 为大量客户机自动分配地址,提供集中管理 减轻管理和维护成本,提高网络配置效率 可分配的地址信息主要包括: 网卡的IP地址.子网掩码 对应的网络地址 默认网关地址 ...

  7. [改善Java代码]适时选择不同的线程池来实现

    Java的线程池实现从最根本上来说只有两个:ThreadPoolExecutor类和ScheduledThreadPoolExecutor类,这两个类还是父子关系,但是Java为了简化并行计算,还提供 ...

  8. [改善Java代码]建议采用的顺序是 List<T>、List<?>、List<Object>

    建议98:建议采用的顺序是 List<T>.List<?>.List<Object> List<T>.List<?>.List<Obj ...

  9. Linux 根文件系统的制作

    一.建立根文件系统目录与文件 1. 创建目录 #mkdir rootfs #cd rootfs #mkdir bin dev etc lib proc sbin sys usr mnt tmp var ...

  10. hdu 3938 Portal

    Portal Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...