CAF(C++ actor framework)使用随笔(各种send通信用法)(三)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了CAF(C++ actor framework)使用随笔(各种send通信用法)(三),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2291字,纯文字阅读大概需要4分钟。
内容图文
c). 同步发送, 等待响应, 超时后收到1个系统消息.
贴上代码
#include <iostream> #include "caf/all.hpp" #include "caf/io/all.hpp" #include <string> #include <thread> #include <chrono> #include <unistd.h> usingnamespace std; usingnamespace caf; behavior fun(event_based_actor* self){ return { [self](conststring& str, const actor &buddy)->string { aout(self)<<str<<endl; //self->delayed_send(buddy,std::chrono::milliseconds(10),"I‘m lated"); //std::this_thread::sleep_for(std::chrono::seconds(1)); //usleep(10000);while(1); return"log"; self->quit(); } }; } void fun1(event_based_actor* self, const actor &buddy){ self->sync_send(buddy,"hi!",self).then( [=](conststring& str) { aout(self)<<str<<endl; }, after(std::chrono::milliseconds(1))>>[&](){ aout(self)<<"timeout!"<<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(); return0; }
其中自己试了几种线程休息的几种方法,发现usleep和 C++11自己提供的线程休眠库会导致coredump并且输出一堆东西,可能是caf自己的东西没有去深究等,
还有就是delay_send我也一开始有点傻,想用delay_send去回复发过来的消息,所以传入了actor,但是要搞清楚“发送”和“回复”是两种东西,你不能用发送来回复,只能用return来回复
d). 同步发送, 同步等待. 适用阻塞的actor api.
#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)->string { aout(self)<<str<<endl; return"I got it."; //self->quit(); } }; } void fun1(blocking_actor* self, actor buddy){ self->sync_send(buddy,"hi!").await( [=](conststring& str) { aout(self)<<str<<endl; }, after(std::chrono::milliseconds(1))>>[&](){ aout(self)<<"timeout!"<<endl; } ); aout(self)<<"i‘m not waiting for you!"<<endl; } int main(){ auto actor1 = spawn(fun); auto actor2 = spawn<blocking_api>(fun1,actor1); caf::await_all_actors_done(); shutdown(); return0; }
结果(顺序变了)为
唯一修改就是spawn那边加上类型。
自己写了一个很简陋的聊天 传文件的程序,有兴趣可以看一下,维护好友列表,心跳机制都是用caf实现,当时没有用caf的序列话 用的是boost库的。
https://github.com/zhejiangxiaomai/chat
原文:http://www.cnblogs.com/zhejiangxiaomai/p/5246810.html
内容总结
以上是互联网集市为您收集整理的CAF(C++ actor framework)使用随笔(各种send通信用法)(三)全部内容,希望文章能够帮你解决CAF(C++ actor framework)使用随笔(各种send通信用法)(三)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。