简体   繁体   English

共享命名内存(Windows)

[英]Shared-Named memory (Windows)

I have recently started a project that requires the use of Shared/Named memory. 我最近开始了一个需要使用共享/命名内存的项目。 I have a working prototype - but before I commit to the current implementation, I would like to learn a little bit more on the subject. 我有一个工作原型 - 但在我承诺当前的实现之前,我想更多地了解这个主题。

I have checked the MSDN docs (and various other sources) and I grasp the basic principles behind how everything works, but I was not able to find answers to my questions below. 我检查了MSDN文档(以及其他各种来源),并掌握了一切如何工作的基本原则,但我无法在下面找到我的问题的答案。

1) If you create a shared memory space and don't provide a valid file handle, it uses the System Page file for its storage. 1)如果创建共享内存空间但未提供有效的文件句柄,则会使用系统页面文件进行存储。 My question is - If I create my own file, and map the view to that file - will the performance be comparatively the same as when mapping to the system page file? 我的问题是 - 如果我创建自己的文件,并将视图映射到该文件 - 性能是否与映射到系统页面文件时的性能相当?

2) You can access the data in the shared memory space by using CopyMemory (which creates a copy of the data) or by casting the result of MapViewOfFile to the type that was written there in the first place. 2)您可以使用CopyMemory(创建数据副本)或将MapViewOfFile的结果转换为首先写入的类型来访问共享内存空间中的数据。 lets assume we wrote a data structure "MyStruct" there. 假设我们在那里写了一个数据结构“MyStruct”。 Is it save to do the following? 是否可以保存以下内容?

auto pReferenceToSharedMemory = (MyStruct*)MapViewOfFile(....);

pReferenceToSharedMemory->SomeField = 12345;
pReferenceToSharedMemory->SomeField2 = ...;
...

Assuming the above is safe to do - its surely more efficient to apply data changes to the data stored in the shared memory space than to copy the data out, change some values, and copy it back? 假设上述操作是安全的 - 将数据更改应用于存储在共享内存空间中的数据肯定比将数据复制出来更新,更改某些值并将其复制回来更有效率?

3) Lastly - how expensive are the OpenFileMapping and MapViewOfFile operations? 3)最后 - OpenFileMapping和MapViewOfFile操作有多贵? I would think that ideally you should only execute OpenFileMapping once (at the beginning of your operation), execute MapViewOfFile once, and use the reference that it returns throughout the operation, rather than executing MapViewOfFile every time you would like to access the data? 我认为理想情况下你应该只执行一次OpenFileMapping(在操作开始时),执行一次MapViewOfFile,并使用它在整个操作中返回的引用,而不是每次要访问数据时都执行MapViewOfFile?

Finally: Is it possible that the reference returned by MapViewOfFile and the data stored in MapViewOfFile to become out of sync ? 最后: MapViewOfFile返回的引用和MapViewOfFile中存储的数据是否可能不同步?

1) The choice between your own file and the system page file isn't performance; 1)您自己的文件和系统页面文件之间的选择不是性能; it's persistence. 这是坚持不懈的。 Changes to your file will still be there next time your program runs. 下次程序运行时,对文件的更改仍将存在。

2) The proper solution would be `new (MapViewOfFile(...)) MyStruct, if the mapping is backed by the page file and therefore still empty. 2)正确的解决方案是`new(MapViewOfFile(...))MyStruct,如果映射由页面文件支持,因此仍为空。

3) The expensive operations are reading and writing, not the meta-operations. 3)昂贵的操作是读写,而不是元操作。

4) I've got no idea what that even means, so I'm fairly certain the answer is NO. 4)我不知道这意味着什么,所以我很确定答案是否定的。

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

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