Question

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.

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. 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. lets assume we wrote a data structure "MyStruct" there. 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? 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?

Finally: Is it possible that the reference returned by MapViewOfFile and the data stored in MapViewOfFile to become out of sync ?

Was it helpful?

Solution

1) The choice between your own file and the system page file isn't performance; 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.

3) The expensive operations are reading and writing, not the meta-operations.

4) I've got no idea what that even means, so I'm fairly certain the answer is NO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top