8

I've long had a desire for an STLish container that I could place into a shared memory segment or a memory mapped file.

I've considered the use of a custom allocator and placement new to place a regular STL container into a shared memory segment. (like this ddj article). The problem is that STL containers will internally have pointers to the memory they own. Therefore, if the shared memory segment or memory mapped file loads at a different base address (perhaps on a subsequent run, or in a second process), then the internal pointers are suddenly invalid. As far as I can figure out, the custom allocator approach only works if you can always map the memory segment into your process at the same address. At least with memory mapped files, I have lots of experience of that NOT being the case if you just let the system map it where ever it feels like.

I've had some thoughts on how to do this, but I'd like to avoid it if someone else has already done the work (that's me, being lazy).

I'm currently leaving locking out of the discussion, as the best locking strategy is highly application dependent.

4

5 回答 5

12

The best starting point for this is probably the boost Interprocess libraries. They have a good example of a map in shared memory here: interprocess map

You will probably also want to read the section on offset smart pointers, which solves the internal pointer problem you were referring to. Offset Pointer

于 2008-11-04T18:48:06.950 回答
0

You may also want to checkout the Intel Threading Building Blocks (TBB) Containers.

于 2008-11-04T19:05:57.257 回答
0

I always had good experiences (years ago) with ACE. Its a networking/communication framework, but has a section on shared memory.

于 2008-11-05T22:40:06.810 回答
0

I only know of proprietary versions. Bloomberg and EA have both published about their STL versions, but havent released ( to my knowledge ) the fruits of their labor.

于 2008-11-10T20:26:57.360 回答
-1

Try using Qt's QSharedMemory Implementation.

于 2009-11-04T09:51:40.363 回答