Message Queue(后文简写成MQ或消息队列)是boost库中用来封装进程间通信的一种实现,同一台机器上的进程或线程可以通过消息队列来进行通迅。消息队列中的消息由优先级、消息长度、消息数据三部分组成。这里需要注意的事,MQ只是简单的将要发送的数据在内存中进行拷贝,所以我们在发送复杂结构或对象时,我们需要将其序列化后再发送,接收端接收时要反序列化,也就是说我们要自己去定义区分一条消息(就是自定义网络通迅协议)。在MQ中,我们可以使用三模式去发送和接收消息:
  1. 阻塞:在发送消息时,若消息队列满了,那么发送接口将会阻塞直到队列没有满。在接收消息时,若队列为空,那么接收接口也会阻塞直到队列不空。
  2. 超时:用户可以自定义超时时间,在超时时间到了,那么发送接口或接收接口都会返回,无论队列满或空
  3. Try:在队列为空或满时,都能立即返回
MQ使用命名的共享内存来实现进程间通信。共享内存换句话来说,就是用户可以指定一个名称来创建一块共享内存,然后像打一个文件一样去打开这块共享内存,同样别的进程也可以根据这个名称来打开这块共享内存,这样一个进程向共享内存中写,另一个进程就可以从共享内存中读。这里两个进程的读写就涉及到同步问题。另外,在创建一个MQ时,我们需要指定MQ的最大消息数量以及消息的最大size。

  1. //Create a message_queue. If the queue
  2. //exists throws an exception
  3. message_queue mq
  4. (create_only         //only create
  5. ,"message_queue"     //name
  6. ,100                 //max message number
  7. ,100                 //max message size
  8. );
  9. using boost::interprocess;
  10. //Creates or opens a message_queue. If the queue
  11. //does not exist creates it, otherwise opens it.
  12. //Message number and size are ignored if the queue
  13. //is opened
  14. message_queue mq
  15. (open_or_create      //open or create
  16. ,"message_queue"     //name
  17. ,100                 //max message number
  18. ,100                 //max message size
  19. );
  20. using boost::interprocess;
  21. //Opens a message_queue. If the queue
  22. //does not exist throws an exception.
  23. message_queue mq
  24. (open_only           //only open
  25. ,"message_queue"     //name
  26. );
使用message_queue::remove("message_queue");来移除一个指定的消息队列。
接下来,我们看一个使用消息队列的生产者与消息者的例子。第一个进程做为生产者,第二个进程做为消费者。
生产者进程:
  1. #include <boost/interprocess/ipc/message_queue.hpp>
  2. #include <iostream>
  3. #include <vector>
  4. using namespace boost::interprocess;
  5. int main ()
  6. {
  7. try{
  8. //Erase previous message queue
  9. message_queue::remove("message_queue");
  10. //Create a message_queue.
  11. message_queue mq
  12. (create_only               //only create
  13. ,"message_queue"           //name
  14. ,100                       //max message number
  15. ,sizeof(int)               //max message size
  16. );
  17. //Send 100 numbers
  18. for(int i = 0; i < 100; ++i){
  19. mq.send(&i, sizeof(i), 0);
  20. }
  21. }
  22. catch(interprocess_exception &ex){
  23. std::cout << ex.what() << std::endl;
  24. return 1;
  25. }
  26. return 0;
  27. }
消费者进程:
  1. #include <boost/interprocess/ipc/message_queue.hpp>
  2. #include <iostream>
  3. #include <vector>
  4. using namespace boost::interprocess;
  5. int main ()
  6. {
  7. try{
  8. //Open a message queue.
  9. message_queue mq
  10. (open_only        //only create
  11. ,"message_queue"  //name
  12. );
  13. unsigned int priority;
  14. message_queue::size_type recvd_size;
  15. //Receive 100 numbers
  16. for(int i = 0; i < 100; ++i){
  17. int number;
  18. mq.receive(&number, sizeof(number), recvd_size, priority);
  19. if(number != i || recvd_size != sizeof(number))
  20. return 1;
  21. }
  22. }
  23. catch(interprocess_exception &ex){
  24. message_queue::remove("message_queue");
  25. std::cout << ex.what() << std::endl;
  26. return 1;
  27. }
  28. message_queue::remove("message_queue");
  29. return 0;
  30. }

详解boost库中的Message Queue .的更多相关文章

  1. 详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)

    一.boost 智能指针 智能指针是利用RAII(Resource Acquisition Is Initialization:资源获取即初始化)来管理资源.关于RAII的讨论可以参考前面的文章.在使 ...

  2. boost库中sleep方法详解

    博客转载自:https://blog.csdn.net/huang_xw/article/details/8453506 boost库中sleep有两个方法: 1. 这个方法只能在线程中用, 在主线程 ...

  3. boost库中的 program_options

    1.阅读rviz中的源码时在rviz/visualizer_app.cpp中遇到如下代码: po::options_description options; options.add_options() ...

  4. 详解 Go 语言中的 time.Duration 类型

    swardsman详解 Go 语言中的 time.Duration 类型swardsman · 2018-03-17 23:10:54 · 5448 次点击 · 预计阅读时间 5 分钟 · 31分钟之 ...

  5. 详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别

    详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别 http://blog.sina.com.cn/s/blog_686999de0100jgda.html   实例: ...

  6. 详解jquery插件中(function ( $, window, document, undefined )的作用。

    1.(function(window,undefined){})(window); Q:(function(window,undefined){})(window);中为什么要将window和unde ...

  7. zz详解深度学习中的Normalization,BN/LN/WN

    详解深度学习中的Normalization,BN/LN/WN 讲得是相当之透彻清晰了 深度神经网络模型训练之难众所周知,其中一个重要的现象就是 Internal Covariate Shift. Ba ...

  8. [转载]详解网络传输中的三张表,MAC地址表、ARP缓存表以及路由表

    [转载]详解网络传输中的三张表,MAC地址表.ARP缓存表以及路由表 虽然学过了计算机网络,但是这部分还是有点乱.正好在网上看到了一篇文章,讲的很透彻,转载过来康康. 本文出自 "邓奇的Bl ...

  9. 详解WebService开发中四个常见问题(2)

    详解WebService开发中四个常见问题(2)   WebService开发中经常会碰到诸如WebService与方法重载.循环引用.数据被穿该等等问题.本文会给大家一些很好的解决方法. AD:WO ...

随机推荐

  1. Win7主题被禁用

    今天早上干了一件傻缺的事,打开电脑的时候,某卫士提醒开机速度击败全国0.2%的电脑,之后点了优化...随后就发生了接下来的一幕: win7下面的主题都不能使用了,只能使用那种复古(很丑的样式,看着很不 ...

  2. [flex & bison]编译器杂谈

    flex与bison是编译器设计工具.这里的编译器为广义,其中包括一般的编译器.脚本解析器等,需要进行语言结构解析来得出意义的程序. 当我们需要用一个语言来设计一款编译器时,需要考虑太多设计重心外的东 ...

  3. Python 命令行非阻塞输入

    很久很久以前,系windows平台下,用C语言写过一款贪食蛇游戏,cmd界面,用kbhit()函数实现非阻塞输入.系windows平台下用python依然可以调用msvcrt.khbit实现非阻塞监听 ...

  4. 1036 : Trie图 (AC自动机)

    题目大意: 输入 n 个目标单词和一个文本串,判断文本串中是否存在某些目标单词. 思路 赤裸裸的 AC自动机. 代码: #include<iostream> #include<cst ...

  5. 基于deb包快速搭建内外apt源

  6. EasyUEFI

    ---------------------------------http://www.easyuefi.com/downloads/EasyUEFI_Setup.exe--------------- ...

  7. get client machine name

    System.Net.Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName;

  8. unicode下各种类型转换,CString,string,char*,int,char[]

    把最近用到的各种unicode下类型转换总结了一下,今后遇到其他的再补充: 1.string转CString string a=”abc”; CString str=CString(a.c_str() ...

  9. DataTable操作工具类DataTableHelper

    DataTable操作工具类DataTableHelper. 功能介绍: 将泛型集合填充为数据表 将泛型填充为数据表 将对象集合填充为数据表 将对象填充为数据表 将定IDictionary数据转换为D ...

  10. C#中MD5加密

    C#中进行MD5加密需要使用MD5这个类,这个类位于System.Security.Cryptography命名空间. 转到元数据得知MD5是抽象类和两个静态方法 上代码详解: //得到其静态方法创建 ...