队列详解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;

}

EdgeNat 新年开通优惠 - 韩国独立服务器原生IP地址CN2线路七折优惠

EdgeNat 商家在之前也有分享过几次活动,主要提供香港和韩国的VPS主机,分别在沙田和首尔LG机房,服务器均为自营硬件,电信CN2线路,移动联通BGP直连,其中VPS主机基于KVM架构,宿主机采用四路E5处理器、raid10+BBU固态硬盘!最高可以提供500Gbps DDoS防御。这次开年活动中有提供七折优惠的韩国独立服务器,原生IP地址CN2线路。第一、优惠券活动EdgeNat优惠码(限月...

水墨云历史黑名单IDC,斟酌选购

水墨云怎么样?本站黑名单idc,有被删除账号风险,建议转出及数据备份!水墨云ink cloud Service是成立于2017年的商家,自2020起开始从事香港、日本、韩国、美国等地区CN2 GIA线路的虚拟服务器租赁,同时还有台湾、国内nat vps相关业务,也有iplc专线产品,相对来说主打的是大带宽服务器产品。注意:本站黑名单IDC,有被删除账号风险,请尽量避免,如果已经购买建议转出及数据备...

MineServer:香港CMI/洛杉矶GIA VPS,2核/2GB内存/20GB NVME/3.5TB流量/200Mbps/KVM,288元/年

mineserver怎么样?mineserver是一家国人商家,主要提供香港CN2 KVM VPS、香港CMI KVM VPS、日本CN2 KVM VPS、洛杉矶cn2 gia端口转发等服务,云服务器网(yuntue.com)介绍过几次,最近比较活跃。现在新推出了3款特价KVM VPS,性价比高,香港CMI/洛杉矶GIA VPS,2核/2GB内存/20GB NVME/3.5TB流量/200Mbps...

boost库为你推荐
海贼王644海贼王第644集什么时候出百度空间首页怎样通过账号登录自己的百度空间腾讯空间首页手机QQ空间首页从哪里进入!316不锈钢和304哪个好304和316不锈钢区别哪个好天玑1000plus和骁龙865哪个好天玑720和骁龙765g哪个好?输入法哪个好用五笔输入法哪个最好用视频剪辑软件哪个好后期视频剪辑什么软件最专业?少儿英语哪个好少儿英语,那个好一些?网页传奇哪个好玩哪个传奇类网页游戏即好玩又不花钱??核芯显卡与独立显卡哪个好独立显卡和核心显卡哪个好
vps是什么 cn域名个人注册 edgecast 天猫双十一秒杀 dropbox网盘 165邮箱 服务器维护方案 ftp免费空间 shuang12 沈阳主机托管 新加坡空间 网络速度 hdchina 亿库 空间排行榜 2016黑色星期五 cc加速器 alertpay 卡巴斯基免费版 2000元电脑主机配置 更多