首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
详解boost库中的Message Queue .
时间:
2015-03-13 12:21:04
阅读:
147
评论:
0
收藏:
0
[点我收藏+]
标签:
Message Queue(后文简写成MQ或消息队列)是boost库中用来封装进程间通信的一种实现,同一台机器上的进程或线程可以通过消息队列来进行通迅。消息队列中的消息由优先级、消息长度、消息数据三部分组成。这里需要注意的事,MQ只是简单的将要发送的数据在内存中进行拷贝,所以我们在发送复杂结构或对象时,我们需要将其序列化后再发送,接收端接收时要反序列化,也就是说我们要自己去
定义区分一条消息(就是自定义网络通迅协议)。
在MQ中,我们可以使用三模式去发送和接收消息:
阻塞:在发送消息时,若消息队列满了,那么发送接口将会阻塞直到队列没有满。在接收消息时,若队列为空,那么接收接口也会阻塞直到队列不空。
超时:用户可以自定义超时时间,在超时时间到了,那么发送接口或接收接口都会返回,无论队列满或空
Try:在队列为空或满时,都能立即返回
MQ使用命名的共享内存来实现进程间通信。共享内存换句话来说,就是用户可以指定一个名称来创建一块共享内存,然后像打一个文件一样去打开这块共享内存,同样别的进程也可以根据这个名称来打开这块共享内存,这样一个进程向共享内存中写,另一个进程就可以从共享内存中读。这里两个进程的读写就涉及到同步问题。另外,
在创建一个MQ时,我们需要指定MQ的最大消息数量以及消息的最大size。
[cpp]
view plain
copy
print
?
//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
);
使用
message_queue
::
remove
(
"message_queue"
);来移除一个指定的消息队列。
接下来,我们看一个使用消息队列的生产者与消息者的例子。第一个进程做为生产者,第二个进程做为消费者。
生产者进程:
[cpp]
view plain
copy
print
?
#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;
}
消费者进程:
[cpp]
view plain
copy
print
?
#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 .
标签:
原文地址:http://www.cnblogs.com/ltm5180/p/4334522.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!