C++进程间通信的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/372198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way for interprocess communication in C++
提问by Qubeuc
I have two processes one will query other for data.There will be huge amount of queries in a limited time (10000 per second) and data (>100 mb) will be transferred per second.Type of data will be an integral type(double,int) My question is in which way to connect this process?
我有两个进程,一个将查询其他数据。在有限的时间内(每秒 10000 次)会有大量查询,并且每秒将传输数据(> 100 mb)。数据类型将是整数类型(双,int) 我的问题是用什么方式连接这个进程?
Shared memory , message queue , lpc(Local Procedure call) or others....
共享内存、消息队列、lpc(本地过程调用)或其他....
And also i want to ask which library you suggest? by the way please do not suggest MPI. edit : under windows xp 32 bit
我也想问你推荐哪个图书馆?顺便说一句,请不要建议 MPI。编辑:在 windows xp 32 位下
回答by Johannes Schaub - litb
One Word: Boost.InterProcess. If it really needs to be fast, shared memory is the way to go. You nearly have zero overhead as the operation system does the usual mapping between virtual and physical addresses and no copy is required for the data. You just have to lookout for concurrency issues.
一个字:Boost.InterProcess。如果真的需要快速,共享内存是不二之选。由于操作系统在虚拟地址和物理地址之间进行通常的映射,并且不需要数据副本,因此您的开销几乎为零。您只需要注意并发问题。
For actually sending commands like shutdownand query, i would use message queues. I previously used localhost network programming to do that, and used manual shared memory allocation, before i knew about boost. Damn if i would need to rewrite the app, i would immediately pick boost. Boost.InterProcess makes this more easy for you. Check it out.
对于实际发送像shutdown和query这样的命令,我会使用消息队列。在我了解 boost 之前,我以前使用 localhost 网络编程来做到这一点,并使用手动共享内存分配。该死的,如果我需要重写应用程序,我会立即选择 boost。Boost.InterProcess 使这对您来说更容易。一探究竟。
回答by Marc
I would use shared memory to store the data, and message queues to send the queries.
我会使用共享内存来存储数据,并使用消息队列来发送查询。
回答by Marc
I'll second Marc's suggestion -- I'd not bother with boost unless you have a portability concern or want to do cool stuff like map standard container types over shared memory (in which case I'd definitely use boost).
我会支持 Marc 的建议——除非你有可移植性的问题或者想要做一些很酷的事情,比如在共享内存上映射标准容器类型(在这种情况下我肯定会使用 boost),否则我不会打扰 boost。
Otherwise, message queues and shared memory are pretty simple to deal with.
否则,消息队列和共享内存很容易处理。
回答by EhevuTov
If your data consists of multiple types and/or you need things like mutex, use Boost. Else use a shared section of memory using #pragma data_seg or a memory mapped file.
如果您的数据包含多种类型和/或您需要互斥锁之类的东西,请使用 Boost。否则使用 #pragma data_seg 或内存映射文件使用共享内存部分。
回答by Greg Rogers
If you do use shared memory you will have to decide whether or not to spin or not. I'd expect that if you use a semaphore for synchronization and storing data in shared memory you will not get much performance benefit compared to using message queues (at significant clarity degradation), but if you spin on an atomic variable for synchronization, then you have to suffer the consequences of that.
如果您确实使用共享内存,则必须决定是否旋转。我希望如果您使用信号量进行同步并将数据存储在共享内存中,与使用消息队列相比,您不会获得太多的性能优势(清晰度显着下降),但是如果您使用原子变量进行同步,那么您必须承受这样的后果。