简体   繁体   English

关于这种情况下消息队列与共享内存的适用性或适用性

[英]Regarding the applicability or suitability of message queue vs. Shared Memory in this situation

This is regarding the applicability or suitability of message queue vs. Shared Memory in this situation: 这是关于在这种情况下消息队列与共享内存的适用性或适用性:

  1. multiple DLLs or shared libraries 多个DLL或共享库

  2. each library will try to communicate with My main application DLL or shared library, eg, the I/P to and O/P from all the DLLs or Shared libraries are to be communicated through my main application's Shared library. 每个库都将尝试与我的主应用程序DLL或共享库通信,例如,所有DLL或共享库的I / P和O / P将通过我的主应用程序的共享库进行通信。 These communications are ASYNCHRONOUS. 这些通信是异步的。

  3. some of the DLLs or shared libraries, other than my application's .so, will create multiple threads, and output from each such thread needs to be communicated back to my application library. 除了我的应用程序的.so之外,某些DLL或共享库将创建多个线程,每个这样的线程的输出都需要传递回我的应用程序库。 The output from these threads are again ASYNCHRONOUS. 这些线程的输出再次是ASYNCHRONOUS。

  4. my main application DLL / .so will continue with its other work which is very likely that it communicates with some server over the network and it responds accordingly 我的主应用程序DLL / .so将继续其其他工作,这很可能是它与网络上的某些服务器进行通信,并因此做出了响应

  5. The functioning of all other DLLs/ .so's are asynchronous 所有其他DLL / .so的功能都是异步的

Q-1: In the above situation which is the best fit ? 问题1:在上述情况下,哪个最合适? (I) a message queue , (II) a shared memory ? (I)消息队列,(II)共享内存?

Q-2: Any reference or link which enforces synchronization between several shared libraries using shared memory ? 问题2:是否有任何引用或链接使用共享内存在多​​个共享库之间强制进行同步?

I guess your understanding of the problem is wrong: 我想您对问题的理解是错误的:

  • Shared memory is a "channel" to connect different processes, like sockets, pipes, or normal memory. 共享内存是连接不同进程(例如套接字,管道或普通内存)的“通道”。
  • A message queue is a "protocol" for passing messages, like TCP or a ring buffer. 消息队列是用于传递消息的“协议”,例如TCP或环形缓冲区。 You may create it over a socket (like 0MQ) or using a synchronized queue (like Intel TBB, see below) in shared or "normal" memory. 您可以通过套接字(如0MQ)或在共享或“正常”内存中使用同步队列(如Intel TBB,请参见下文)来创建它。

You do not need shared memory with the specifications you give. 您不需要共享具有给定规格的内存。 Shared memory is useful if one of the following is true: 如果满足以下条件之一,则共享内存很有用:

  • You have several processes (you don't, all your so/dlls will share the same memory) 您有几个进程(您没有,所有的so / dll将共享相同的内存)
  • You need to persist the memory of your process if it crashes (you may need that but didn't mention). 如果进程崩溃,则需要保留该进程的内存(您可能需要,但未提及)。

Now, you need to choose a protocol for your code to talk over it. 现在,您需要为您的代码选择协议以进行讨论。 I'd recommend using Intel Thread Building Blocks ( TBB , that would answer Q2). 我建议使用Intel Thread Building Blocks( TBB ,它将回答Q2)。 They provide different layers of abstractions for what you want to achieve, I do not know enough to choose for you, though, take some time to read the (long) docs. 它们为您要实现的目标提供了不同的抽象层,尽管您花些时间阅读(冗长的)文档,但我所知还不足以为您选择。

With the caveat that I only understand your application in the most general sense, I think message queues are a no-brainer provided the msgs you are passing are bounded and appropriate for a queue to being with. 需要说明的是,我仅从最一般的意义上理解您的应用程序,因此我认为消息队列是不费吹灰之力, 只要您传递的msgs有界且适合与队列一起使用。

The two primary things you seem to be concerned with are synchronization and asynchronousity. 您似乎关心的两个主要方面是同步和异步。 (1) POSIX message queues have the queue synchronization already built-in. (1)POSIX消息队列已内置队列同步。 That's one big headache removed. 那是消除的一大头疼。 (2) Under Linux, the queue id mqd_t is a file descriptor which means it can be used in a select statement. (2)在Linux下,队列ID mqd_t是文件描述符,这意味着它可以在select语句中使用。 That takes care of the asynchronous concerns. 这就解决了异步问题。 In your main DLL you can load up the mqd_t descriptors for all your queues in select statement and process your DLL msgs as they arrive with a consistent, debugged and well understood mechanism. 在主DLL中,您可以在select语句中为所有队列加载mqd_t描述符,并在它们到达时以一致,调试和易于理解的机制处理DLL消息。 (3) The tiny bit (and it is tiny for most apps) of efficiency you lose compared to shared memory is more than made up by the relative ease of msg queue use and the fact that your main application DLL is going to spend a relatively long time waiting on network I/O with the server anyway. (3)与共享内存相比,您损失的一点点效率(对于大多数应用程序来说都是微不足道的),远远超过了msg队列使用的相对简便性以及您的主应用程序DLL将花费相对较少的事实所弥补。无论如何,长时间等待服务器的网络I / O。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM