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

}

Vultr再次发布充值多少送多少活动

昨天我们很多小伙伴们应该都有看到,包括有隔壁的一些博主们都有发布Vultr商家新的新用户注册福利活动。以前是有赠送100美元有效期30天的,这次改成有效期14天。早年才开始的时候有效期是60天的,这个是商家行为,主要还是吸引到我们后续的充值使用,毕竟他们的体验金赠送,在同类商家中算是比较大方的。昨天活动内容:重新调整Vultr新注册用户赠送100美元奖励金有效期14天今天早上群里的朋友告诉我,两年...

腾讯云2核4GB内存8M带宽 年74元

一般大厂都是通过首年才有可以享受爆款活动,然后吸引我们注册他们商家达到持续续费和购买的目的。一般只有大厂才能有这样的魄力和能力首年亏本,但是对于一般的公司和个人厂家确实难过,这几年确实看到不少的同类商家难以生存。这里我们可以看到有对应的套餐方案。不过这两个套餐都是100%CPU独享的,不是有某云商家限制CPU的。但是轻量服务器有个不好的就是带宽是较大且流量是限制的额,分别是1GB和1.2TB月流量...

VirMach:$7.2/年KVM-美元512MB/$7.2/年MB多个机房个机房可选_双线服务器租赁

Virmach对资源限制比较严格,建议查看TOS,自己做好限制,优点是稳定。 vCPU 内存 空间 流量 带宽 IPv4 价格 购买 1 512MB 15GB SSD 500GB 1Gbps 1 $7/VirMach:$7/年/512MB内存/15GB SSD空间/500GB流量/1Gbps端口/KVM/洛杉矶/西雅图/芝加哥/纽约等 发布于 5个月前 (01-05) VirMach,美国老牌、稳...

boost库为你推荐
软银赛富赛富通的发展历程软银孙正义马云富还是孙正义富美女桌面背景图片适合女生的电脑壁纸聚酯纤维和棉哪个好聚酯纤维面料和纯棉面料哪个好华为p40和mate30哪个好mate30 5G和p40 5G有什么区别?苹果手机助手哪个好苹果手机助手哪个好电陶炉和电磁炉哪个好电陶炉和电磁炉哪个好电陶炉和电磁炉哪个好电磁炉跟电陶炉哪个好手机炒股软件哪个好手机炒股软件播放器哪个好哪个播放器最好
江西服务器租用 最新代理服务器ip 免费注册网站域名 域名解析文件 com域名抢注 服务器评测 主机 私服服务器 godaddy续费优惠码 好看的留言 阿里云代金券 云主机51web 英文站群 卡巴斯基永久免费版 促正网秒杀 双11秒杀 adroit 免费活动 空间合租 免费测手机号 更多