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

}

青云互联-洛杉矶CN2弹性云限时五折,9.5元/月起,三网CN2gia回程,可选Windows,可自定义配置

官方网站:点击访问青云互联官网优惠码:五折优惠码:5LHbEhaS (一次性五折,可月付、季付、半年付、年付)活动方案:的套餐分为大带宽限流和小带宽不限流两种套餐,全部为KVM虚拟架构,而且配置都可以弹性设置1、洛杉矶cera机房三网回程cn2gia 洛杉矶cera机房                ...

AkkoCloud(60元/月 ),英国伦敦CN2 1核 768 MB 内存 10 GB SSD 硬盘 600GB 流量 英国伦敦CN2 1核  1.5G  300Mbps

官方网站:https://www.akkocloud.com/AkkoCloud新品英国伦敦CN2 GIA已上线三网回程CN2 GIA 国内速度优秀.电信去程CN2 GIALooking Glass:http://lonlg.akkocloud.com/Speedtest:http://lonlg.akkocloud.com/speedtest/新品上线刚好碰上国庆节 特此放上国庆专属九折循环优惠...

bgpto:独立服务器夏季促销,日本机器6.5折、新加坡7.5折,20M带宽,低至$93/月

bgp.to对日本机房、新加坡机房的独立服务器在搞特价促销,日本独立服务器低至6.5折优惠,新加坡独立服务器低至7.5折优惠,所有优惠都是循环的,终身不涨价。服务器不限制流量,支持升级带宽,免费支持Linux和Windows server中文版(还包括Windows 10). 特色:自动部署,无需人工干预,用户可以在后台自己重装系统、重启、关机等操作!官方网站:https://www.bgp.to...

boost库为你推荐
麒麟990和骁龙865哪个好骁龙八65和麒麟990谁强一点?电脑管家和360哪个好电脑管家和360卫士哪个好?录音软件哪个好好用的录音软件! 急!!手机炒股软件哪个好手机炒股软件网络机顶盒哪个好什么牌子的网络机顶盒好用?群空间登录群空间无法正常登陆的问题yy空间登录yy空间怎么上传照片?辽宁联通网上营业厅中国联通网上营业厅固定电话费查询辽宁联通网上营业厅网联通宽带好不好用360云盘36t什么云盘最大?(看见360云盘好像说有36T,百度云盘好像是2T)
美国网站空间 yaokan永久域名经常更换 windows虚机 vps代购 备案域名出售 主机优惠码 fastdomain webhosting 国外服务器网站 光棍节日志 ssh帐号 免费静态空间 免费全能空间 三拼域名 华为网络硬盘 合租空间 服务器维护方案 空间技术网 台湾谷歌 web服务器是什么 更多