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;
}
日前,国内知名主机服务商阿里云与国外资深服务器面板Plesk强强联合,推出 阿里云域名注册与备案、服务器ECS购买与登录使用 前言云服务器(Elastic 只需要确定cpu内存与带宽基本上就可以了,对于新手用户来说,我们在购买阿里云服务申请服务器与域名许多云服务商的云服务器配置是弹性的 三周学会小程序第三讲:服务 不过这个国外服务器有点慢,可以考虑国内的ngrokcc。 ngrokcc...
达州创梦网络怎么样,达州创梦网络公司位于四川省达州市,属于四川本地企业,资质齐全,IDC/ISP均有,从创梦网络这边租的服务器均可以备案,属于一手资源,高防机柜、大带宽、高防IP业务,一手整C IP段,四川电信,一手四川托管服务商,成都优化线路,机柜租用、服务器云服务器租用,适合建站做游戏,不须要在套CDN,全国访问快,直连省骨干,大网封UDP,无视UDP攻击,机房集群高达1.2TB,单机可提供1...
ProfitServer已开启了黑色星期五的促销活动,一直到本月底,商家新加坡、荷兰、德国和西班牙机房VPS直接5折,无码直购最低每月2.88美元起,不限制流量,提供IPv4+IPv6。这是一家始于2003年的俄罗斯主机商,提供虚拟主机、VPS、独立服务器、SSL证书、域名等产品,可选数据中心包括俄罗斯、法国、荷兰、美国、新加坡、拉脱维亚、捷克、保加利亚等多个国家和地区。我们随便以一个数据中心为例...