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. sed命令查找<media/msm_cam_sensor.h>替换为"len_msm_cam_sensor.h"

    sed -i 's:<media/msm_cam_sensor.h>:"len_msm_cam_sensor.h":g' $(find . -name "*. ...

  2. Android常用代码集合

    这篇文章主要记录一些常用的一些代码段,方便以后查阅,不断更新中. 1:调用浏览器,载入某网址 1 2 3 Uri uri = Uri.parse("http://www.android-st ...

  3. System.exit()方法的作用

    查看java.lang.System的源码.我们能够看到System.exit()这种方法等价于Runtime.exit(),代码例如以下: /** * Terminates the currentl ...

  4. 判断IE中iframe完美加载完毕的方法

    转: var iframe = document.createElement("iframe"); iframe.src = "http://www.planabc.ne ...

  5. 利用nf_conntrack机制存储路由,省去每包路由查找

    IP是无连接的,因此IP路由是每包一路由的,数据包通过查找路由表获取路由,这是现代操作协议协议栈IP路由的默认处理方式.可是假设协议栈具有流识别能力,是不是能够基于流来路由呢?答案无疑是肯定的. 设计 ...

  6. Eclipse对printf()不能输出到控制台的解决方法

    方案1: 在main 语句中加一条 setbuf(stdout,NULL); 这个即可. 在ecplise下使用cdt开发c程序,发现运行后终端没有输出,停止后会输出,通过在main中添加 setbu ...

  7. vs2010 编译Qt5.2 rc1

    首先要准备一些依赖: 下载Qt 5.2.0 rc版的源码 qt-everywhere-opensource-src-5.2.0-rc1.7z 并解压出来, 我的路径为D:\qt5\qt-src-5.2 ...

  8. PHP.2-LAMP平台介绍及网站的工作原理

    LAMP平台介绍及网站的工作原理 1.HTTP协议 URL(UniformResourceLocator)统一资源定位符,就是网页地址的意思.[格式:协议://主机.端口.文件.附加资源] ##URL ...

  9. java中的静态方法

    静态方法是属于类的,内存必须为它分配内存空间,这个空间一直由静态方法占用,内存管理器不会由于静态方法没有被调用而将静态方法的存储空间收回,这样如果将所有的方法都声明为静态方法,就会占用大量的内存空间, ...

  10. C++之类的构造函数,不得不学明白的重点

    一.认识构造函数 当创建一个类类型对象时,类通过一个或者几个特殊的成员函数来控制对象的初始化,这种函数就是构造函数.它的任务就是用来初始化类对象的成员的,所以当创建类对象或者类对象被创建就会调用构造函 ...