详解boost库中的Message Queue .
- 阻塞:在发送消息时,若消息队列满了,那么发送接口将会阻塞直到队列没有满。在接收消息时,若队列为空,那么接收接口也会阻塞直到队列不空。
- 超时:用户可以自定义超时时间,在超时时间到了,那么发送接口或接收接口都会返回,无论队列满或空
- Try:在队列为空或满时,都能立即返回
- //Create a message_queue. If the queue
- //exists throws an exception
- message_queue mq
- (create_only //only create
- ,"message_queue" //name
- ,100 //max message number
- ,100 //max message size
- );
- using boost::interprocess;
- //Creates or opens a message_queue. If the queue
- //does not exist creates it, otherwise opens it.
- //Message number and size are ignored if the queue
- //is opened
- message_queue mq
- (open_or_create //open or create
- ,"message_queue" //name
- ,100 //max message number
- ,100 //max message size
- );
- using boost::interprocess;
- //Opens a message_queue. If the queue
- //does not exist throws an exception.
- message_queue mq
- (open_only //only open
- ,"message_queue" //name
- );
- #include <boost/interprocess/ipc/message_queue.hpp>
- #include <iostream>
- #include <vector>
- using namespace boost::interprocess;
- int main ()
- {
- try{
- //Erase previous message queue
- message_queue::remove("message_queue");
- //Create a message_queue.
- message_queue mq
- (create_only //only create
- ,"message_queue" //name
- ,100 //max message number
- ,sizeof(int) //max message size
- );
- //Send 100 numbers
- for(int i = 0; i < 100; ++i){
- mq.send(&i, sizeof(i), 0);
- }
- }
- catch(interprocess_exception &ex){
- std::cout << ex.what() << std::endl;
- return 1;
- }
- return 0;
- }
消费者进程:
- #include <boost/interprocess/ipc/message_queue.hpp>
- #include <iostream>
- #include <vector>
- using namespace boost::interprocess;
- int main ()
- {
- try{
- //Open a message queue.
- message_queue mq
- (open_only //only create
- ,"message_queue" //name
- );
- unsigned int priority;
- message_queue::size_type recvd_size;
- //Receive 100 numbers
- for(int i = 0; i < 100; ++i){
- int number;
- mq.receive(&number, sizeof(number), recvd_size, priority);
- if(number != i || recvd_size != sizeof(number))
- return 1;
- }
- }
- catch(interprocess_exception &ex){
- message_queue::remove("message_queue");
- std::cout << ex.what() << std::endl;
- return 1;
- }
- message_queue::remove("message_queue");
- return 0;
- }
详解boost库中的Message Queue .的更多相关文章
- 详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
一.boost 智能指针 智能指针是利用RAII(Resource Acquisition Is Initialization:资源获取即初始化)来管理资源.关于RAII的讨论可以参考前面的文章.在使 ...
- boost库中sleep方法详解
博客转载自:https://blog.csdn.net/huang_xw/article/details/8453506 boost库中sleep有两个方法: 1. 这个方法只能在线程中用, 在主线程 ...
- boost库中的 program_options
1.阅读rviz中的源码时在rviz/visualizer_app.cpp中遇到如下代码: po::options_description options; options.add_options() ...
- 详解 Go 语言中的 time.Duration 类型
swardsman详解 Go 语言中的 time.Duration 类型swardsman · 2018-03-17 23:10:54 · 5448 次点击 · 预计阅读时间 5 分钟 · 31分钟之 ...
- 详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别 http://blog.sina.com.cn/s/blog_686999de0100jgda.html 实例: ...
- 详解jquery插件中(function ( $, window, document, undefined )的作用。
1.(function(window,undefined){})(window); Q:(function(window,undefined){})(window);中为什么要将window和unde ...
- zz详解深度学习中的Normalization,BN/LN/WN
详解深度学习中的Normalization,BN/LN/WN 讲得是相当之透彻清晰了 深度神经网络模型训练之难众所周知,其中一个重要的现象就是 Internal Covariate Shift. Ba ...
- [转载]详解网络传输中的三张表,MAC地址表、ARP缓存表以及路由表
[转载]详解网络传输中的三张表,MAC地址表.ARP缓存表以及路由表 虽然学过了计算机网络,但是这部分还是有点乱.正好在网上看到了一篇文章,讲的很透彻,转载过来康康. 本文出自 "邓奇的Bl ...
- 详解WebService开发中四个常见问题(2)
详解WebService开发中四个常见问题(2) WebService开发中经常会碰到诸如WebService与方法重载.循环引用.数据被穿该等等问题.本文会给大家一些很好的解决方法. AD:WO ...
随机推荐
- hdu 4545 魔法串
http://acm.hdu.edu.cn/showproblem.php?pid=4545 #include <cstdio> #include <cstring> #inc ...
- Android URI简介
就Android平台而言,URI主要分三个部分:scheme, authority and path.其中authority又分为host和port.格式如下:scheme://host:port/p ...
- 14.1.3 检查InnoDB 可用性:
14.1.3 Checking InnoDB Availability 14.1.3 检查InnoDB 可用性: 确定是否你的server 支持InnoDB: 1.执行命令 SHOW ENGINES; ...
- COJ 1006 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=979 WZJ的数据结构(六) 难度级别:D: 运行时间限制:1000ms: ...
- UVa 11292 - Dragon of Loowater(排序贪心)
Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.The shore ...
- Linux下的memset函数
函数原型 void *memset(void *s, int c, size_t n); 函数功能 将以s为首的存储空间前n字节空间全部替换为参数c指定的数据. 返回值 更新后的首地址s. 头文件 # ...
- LinGo:疏散问题——线性规划,0-1规划
个部门(A.B.C.D.E)组成.现要将它的几个部门迁出甲市,迁至乙市或丙市. (每个城市最多接纳三个部门) 除去因政府鼓励这样做以外,还有用房便宜,招工方便等好处.对这些好处已作出数量估计,其值如下 ...
- 《鸟哥Linux私房菜基础学习篇》命令索引
在学习的过程,由于很多命令平时都用不着,因此做这个索引方便需要时查找.这包括了前两部分.主要是按页码顺序. P118 date:显示日期与时间 cal:显示日历 bc:计算器 P121 [Tab]:命 ...
- GitHub for Mac
GitHub for Mac 安装 1.从 mac.github.com 下载最新版本的 GitHub. 2.当你开启软件时,你可以选择用你的 GitHub 账户登录,或者新建一个账户. 3.在左侧, ...
- Ubuntu上安装QQ2015
先不卖关子直接上图:Ubuntu 14.04.5 LTS Deb包下载地址: http://www.longene.org/download/WineQQ7.8-20151109-Longene.de ...