队列详解boost库中的Message Queue

boost库  时间:2021-01-26  阅读:()

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;

}

Atcloud:全场8折优惠,美国/加拿大/英国/法国/德国/新加坡vps,500g大硬盘/2T流量/480G高防vps,$4/月

atcloud怎么样?atcloud刚刚发布了最新的8折优惠码,该商家主要提供常规cloud(VPS)和storage(大硬盘存储)系列VPS,其数据中心分布在美国(俄勒冈、弗吉尼亚)、加拿大、英国、法国、德国、新加坡,所有VPS默认提供480Gbps的超高DDoS防御。Atcloud高防VPS。atcloud.net,2020年成立,主要提供基于KVM虚拟架构的VPS、只能DNS解析、域名、SS...

Vultr VPS新增第18个数据中心 瑞典斯德哥尔摩欧洲VPS主机机房

前几天还在和做外贸业务的网友聊着有哪些欧洲机房的云服务器、VPS商家值得选择的。其中介绍他选择的还是我们熟悉的Vultr VPS服务商,拥有比较多达到17个数据中心,这不今天在登录VULTR商家的时候看到消息又新增一个新的机房。这算是第18个数据中心,也是欧洲VPS主机,地区是瑞典斯德哥尔摩。如果我们有需要欧洲机房的朋友现在就可以看到开通的机房中有可以选择瑞典机房。目前欧洲已经有五个机房可以选择,...

iHostART:罗马尼亚VPS/无视DMCA抗投诉vps;2核4G/40GB SSD/100M端口月流量2TB,€20/年

ihostart怎么样?ihostart是一家国外新商家,主要提供cPanel主机、KVM VPS、大硬盘存储VPS和独立服务器,数据中心位于罗马尼亚,官方明确说明无视DMCA,对版权内容较为宽松。有需要的可以关注一下。目前,iHostART给出了罗马尼亚vps的优惠信息,罗马尼亚VPS无视DMCA、抗投诉vps/2核4G内存/40GB SSD/100M端口月流量2TB,€20/年。点击直达:ih...

boost库为你推荐
相机内存卡数据恢复相机储存卡数据丢失怎么找回985和211哪个好985大学好 还是211是什么意思炒股软件哪个好用股票交易软件哪个好?车险哪个好车险平安和人保哪个好?都有什么优点和缺点?oppo和vivo哪个好买oppo手机好还是vivo的好?云盘哪个好免费的网盘哪个好?更大、更安全、更实用?视频软件哪个好手机看视频用什么软件好q空间登录QQ空间经常提示要登录?q空间登录腾讯qq空间登陆YunOS智能电视显示yunos这个字什么意思?
国外服务器租用 中文国际域名 免费域名空间申请 美国vps 本网站服务器在美国维护 堪萨斯服务器 t牌 webhostingpad mach5 私服服务器 godaddy域名优惠码 名片模板psd 鲜果阅读 日本空间 彩虹ip 最好的空间 个人域名 91vps 网站卫士 南通服务器 更多