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;
}
mineserver怎么样?mineserver是一家国人商家,主要提供香港CN2 KVM VPS、香港CMI KVM VPS、日本CN2 KVM VPS、洛杉矶cn2 gia端口转发等服务,云服务器网(yuntue.com)介绍过几次,最近比较活跃。现在新推出了3款特价KVM VPS,性价比高,香港CMI/洛杉矶GIA VPS,2核/2GB内存/20GB NVME/3.5TB流量/200Mbps...
老薛主机怎么样?老薛主机这个商家有存在有一些年头。如果没有记错的话,早年老薛主机是做虚拟主机业务的,还算不错在异常激烈的市场中生存到现在,应该算是在众多商家中早期积累到一定的用户群的,主打小众个人网站业务所以能持续到现在。这不,站长看到商家有在进行夏季促销,比如我们很多网友可能有需要的香港vps主机季度及以上可以半价优惠,如果有在选择不同主机商的香港机房的可以看看老薛主机商家的香港vps。点击进入...
如今我们网友可能较多的会选择云服务器、VPS主机,对于虚拟主机的话可能很多人不会选择。但是我们有些外贸业务用途的建站项目还是会有选择虚拟主机的。今天看到的Stablehost 商家虚拟主机在黑五期间也有四折优惠,对于这个服务商而言不是特别的喜欢,虽然他们商家和我们熟悉的老鹰主机商有些类似,且在后来老鹰主机改版和方案后,Stablehost 商家也会跟随改版,但是性价比认为不如老鹰主机。这次黑色星期...