Message Queue后文简写成MQ戒消息队列是boost库中用来封装进程间通信的一种实现同一台机器上的进程戒线程可以通过消息队列来进行通迅。消息队列中的消息由优先级、消息长度、消息数据三部分组成。这里需要注意的事MQ叧是简单的将要发送的数据在内存中进行拷贝所以我们在发送复杂结构戒对象时我们需要将其序列化后再发送接收端接收时要反序列化也就是说我们要自己去定义区分一条消息就是自定义网络通迅协议 。在MQ中我们可以使用三模式去发送和接收消息
1. 阻塞在发送消息时若消息队列满了那么发送接口将会阻塞直到队列没有满。
在接收消息时若队列为空那么接收接口也会阻塞直到队列不空。
2.超时用户可以自定义超时时间在超时时间到了那么发送接口或接收接口都会
返回无论队列满或空
3. T ry在队列为空或满时都能立即返回
MQ使用命名的共享内存来实现进程间通信。共享内存换句话来说就是用户可以指定一个名称来创建一块共享内存然后像打一个文件一样去打开这块共享内存同样别的进程也可以根据这个名称来打开这块共享内存这样一个进程向共享内存中写另一个进程就可以从共享内存中读。这里两个进程的读写就涉及到同步问题。另外在创建一个MQ时我们需要指定MQ的最大消息数量以及消息的最大size。
//Create a message_queue. If the queue
//exists throws an exceptionmessage_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 openedmessage_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") ;来移除一个指定的消息队列。
接下来我们看一个使用消息队列的生产者与消息者的例子。第一个进程做为生产者第二个进程做为消费者。
生产者进程
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>using namespace boost: :interprocess;int main ( )
{try{
//Erase previous message queuemessage_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 numbersfor(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 numbersfor(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;
}
港云网络官方网站商家简介港云网络成立于2016年,拥有IDC/ISP/云计算资质,是正规的IDC公司,我们采用优质硬件和网络,为客户提供高速、稳定的云计算服务。公司拥有一流的技术团队,提供7*24小时1对1售后服务,让您无后顾之忧。我们目前提供高防空间、云服务器、物理服务器,高防IP等众多产品,为您提供轻松上云、安全防护。点击进入港云网络官方网站港云网络中秋福利1元领【每人限量1台】,售完下架,活...
hostwebis怎么样?hostwebis昨天在webhosting发布了几款美国高配置大硬盘机器,但报价需要联系客服。看了下该商家的其它产品,发现几款美国服务器、法国服务器还比较实惠,100Mbps不限流量,高配置大硬盘,$44/月起,有兴趣的可以关注一下。HostWebis是一家国外主机品牌,官网宣称1998年就成立了,根据目标市场的不同,以不同品牌名称提供网络托管服务。2003年,通过与W...
HostYun是一家成立于2008年的VPS主机品牌,原主机分享组织(hostshare.cn),商家以提供低端廉价VPS产品而广为人知,是小成本投入学习练手首选,主要提供基于XEN和KVM架构VPS主机,数据中心包括中国香港、日本、德国、韩国和美国的多个地区,大部分机房为国内直连或者CN2等优质线路。本月商家全场9折优惠码仍然有效,以KVM架构产品为例,优惠后韩国VPS月付13.5元起,日本东京...