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;
}
在之前几个月中也有陆续提到两次HostYun主机商,这个商家前身是我们可能有些网友熟悉的主机分享团队的,后来改名称的。目前这个品牌主营低价便宜VPS主机,这次有可以看到推出廉价版本的美国CN2 GIA VPS主机,月费地址15元,适合有需要入门级且需要便宜的用户。第一、廉价版美国CN2 GIA VPS主机方案我们可看到这个类型的VPS目前三网都走CN2 GIA网络,而且是原生IP。根据信息可能后续...
HostKvm又上新了,这次上架了2个线路产品:俄罗斯和香港高防VPS,其中俄罗斯经测试电信CN2线路,而香港高防VPS提供30Gbps攻击防御。HostKvm是一家成立于2013年的国外主机服务商,主要提供基于KVM架构的VPS主机,可选数据中心包括日本、新加坡、韩国、美国、中国香港等多个地区机房,均为国内直连或优化线路,延迟较低,适合建站或者远程办公等。俄罗斯VPSCPU:1core内存:2G...
官方网站:点击访问火数云活动官网活动方案:CPU内存硬盘带宽流量架构IP机房价格购买地址4核4G50G 高效云盘20Mbps独享不限openstack1个九江287元/月立即抢购4核8G50G 高效云盘20Mbps独享不限openstack1个九江329元/月立即抢购2核2G50G 高效云盘5Mbps独享不限openstack1个大连15.9元/月立即抢购2核4G50G 高效云盘5Mbps独享不限...