Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyone have a good shared memory container for C++?

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.

like image 740
Michael Kohne Avatar asked Nov 04 '08 17:11

Michael Kohne


1 Answers

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

like image 176
grieve Avatar answered Sep 21 '22 02:09

grieve