CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2069字,纯文字阅读大概需要3分钟。
内容图文
![CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)](/upload/InfoBanner/zyjiaocheng/1316/b26d444373c740bcb559ae9f09310d94.jpg)
e). 消息延迟发送(和前面没太大区别直接上代码)
#include <iostream> #include "caf/all.hpp" #include "caf/io/all.hpp" #include <string> #include <chrono> usingnamespace std; usingnamespace caf; behavior fun(event_based_actor* self){ return { [self](conststring& 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(1), "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(); return0; }
结果为
f). 消息前转(消息转发)forward.
贴上代码
#include <iostream> #include "caf/all.hpp" #include "caf/io/all.hpp" #include <string> #include <chrono> usingnamespace std; usingnamespace caf; behavior fun2(event_based_actor* self){ return { [self](conststring& 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 { [=](conststring& 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( [=](conststring& 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(); return0; }
结果为
原文:http://www.cnblogs.com/zhejiangxiaomai/p/5247529.html
内容总结
以上是互联网集市为您收集整理的CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)全部内容,希望文章能够帮你解决CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。