C++ Windows 7 上最快的 IPC 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7127242/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:29:31  来源:igfitidea点击:

Fastest IPC method on Windows 7

c++cwindowsboostipc

提问by Cartesius00

What is the fastest possible Interprocess Communication (IPC) method on Windows 7? We would like to share only a memory blocks (two-way).

Windows 7 上最快的进程间通信 (IPC) 方法是什么?我们只想共享一个内存块(双向)。

Is it ReadProcessMemoryor something else? We would like to use plain Cbut, for example, what does Boost library use for IPC?

是它ReadProcessMemory还是别的什么?我们想使用普通的,C但是,例如,Boost 库对 IPC 使用什么?

回答by Matteo Italia

ReadProcessMemoryshouldn't even be listed as an IPC method; yes, it can be used as such, but it exists mainly for debugging purposes (if you check its reference, it's under the category "Debugging functions"), and it's surely slower than "real" shared memory because it copiesthe memory of a process into the specified buffer, while real shared memory doesn't have this overhead.

ReadProcessMemory甚至不应该被列为 IPC 方法;是的,它可以这样使用,但它主要用于调试目的(如果你检查它的参考,它在“调试函数”类别下),而且它肯定比“真实”共享内存慢,因为它复制了一个进程进入指定的缓冲区,而真正的共享内存没有这个开销。

The full list of IPC methods supported by Windows is available on the MSDN; still, if you just have two applications that want to share a memory block, you should create a named memory-mapped file (backed by the paging file) with CreateFileMapping/MapViewOfFile, that should be the most straightforward and fastest method. The details of file mapping are described on its pageon MSDN.

Windows 支持的 IPC 方法的完整列表可在 MSDN 上找到;不过,如果你只是希望共享一个内存块的两个应用程序,你应该创建一个名为内存映射文件(由分页文件支持)与CreateFileMapping/ MapViewOfFile,这应该是最直接和最快的方法。文件映射的详细信息在 MSDN上的页面上进行了描述。

The relevant Boost IPC classescan act as a thin wrapper around shared memory, AFAIK it only encapsulates the calls to the relevant system-specific APIs, but in the end you get the usual pointer to the shared memory block, so operation should be as fast as using the native APIs.

相关升压IPC类可以作为一个简单包装的共享内存,据我所知它只是封装了相关的特定系统的API调用,但最终你得到的通常的指针到共享内存块,因此操作应尽可能快就像使用本机 API 一样。

Because of this I advise you to use Boost.Interprocess, since it's portable, C++-friendly (it provides RAII semantics) and does not give you any performance penalty after the shared memory block has been created (it canprovide additional functionalities on shared memory, but they are all opt-in - if you just want shared memory you get just it).

因此,我建议您使用Boost.Interprocess,因为它是可移植的、C++ 友好的(它提供 RAII 语义)并且在创建共享内存块后不会给您任何性能损失(它可以在共享内存上提供附加功能,但它们都是可选的 - 如果您只想要共享内存,您就可以得到它)。