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

}

ManSora:英国CN2 VPS,1核/1GB内存/10GB SSD/1TB流量/100Mbps/KVM,$18.2/月

mansora怎么样?mansora是一家国人商家,主要提供沪韩IEPL、沪日IEPL、深港IEPL等专线VPS。现在新推出了英国CN2 KVM VPS,线路为AS4809 AS9929,可解锁 Netflix,并有永久8折优惠。英国CN2 VPS,$18.2/月/1GB内存/10GB SSD空间/1TB流量/100Mbps端口/KVM,有需要的可以关注一下。点击进入:mansora官方网站地址m...

腾讯云轻量服务器老用户续费优惠和老用户复购活动

继阿里云服务商推出轻量服务器后,腾讯云这两年对于轻量服务器的推广力度还是比较大的。实际上对于我们大部分网友用户来说,轻量服务器对于我们网站和一般的业务来说是绝对够用的。反而有些时候轻量服务器的带宽比CVM云服务器够大,配置也够好,更有是价格也便宜,所以对于初期的网站业务来说轻量服务器是够用的。这几天UCLOUD优刻得香港服务器稳定性不佳,于是有网友也在考虑搬迁到腾讯云服务器商家,对于轻量服务器官方...

ParkinHost:俄罗斯离岸主机,抗投诉VPS,200Mbps带宽/莫斯科CN2线路/不限流量/无视DMCA/55折促销26.4欧元 /年起

外贸主机哪家好?抗投诉VPS哪家好?无视DMCA。ParkinHost今年还没有搞过促销,这次parkinhost俄罗斯机房上新服务器,母机采用2个E5-2680v3处理器、128G内存、RAID10硬盘、2Gbps上行线路。具体到VPS全部200Mbps带宽,除了最便宜的套餐限制流量之外,其他的全部是无限流量VPS。ParkinHost,成立于 2013 年,印度主机商,隶属于 DiggDigi...

boost库为你推荐
软银巨亏除了IPO,私募股权投资还有哪些退出方式苹果x和xr哪个好苹果x,苹果xs,苹果xr,苹果xs max哪个更值得买?985和211哪个好985大学好 还是211是什么意思车险哪个好买汽车保险,买哪几种比较好云盘哪个好免费的网盘哪个实用?美国国际集团全球500强有哪些企业是美国的51个人空间登录为什么登陆51博客个人空间就不能登陆QQ如何增加百度收录如何提高百度收录率?360云盘同步版360云盘和360同步版区别月抛隐形眼镜月抛隐形眼镜大概多少钱
济南域名注册 七牛优惠码 t楼 uk2 国外空间 新站长网 php免费空间 panel1 三拼域名 150邮箱 有奖调查 合租空间 南通服务器 免费高速空间 昆明蜗牛家 如何注册阿里云邮箱 根服务器 万网空间管理 免费ftp atom处理器 更多