-
STL in Shared Memory
Hello everyone,
First of all, sorry for my bad english..
I'm trying to put a hash_map into a shared memory segment. I know all the issues about shared memory containing STL containers (google is full of it), but my hashmap would be preallocated and fixed size, I just need to share it so all other apps can access data from it, no changes would be made.
Can anyone give me some hints, or a simple code part on how to do that !?
Best regards,
AlenDev
-
It would be preferable if you used a container that is actually specified in the C++ standard (or, if you want to push the edge a bit, that is likely to be specified in an upcoming release of the standard). hash_map is not standard.
In any event, if you look at the specification of hash_map (with those compilers/libraries that supply it) or any of the standard containers, you will note one of the template parameters is an allocator type.
All you need to do is create a custom allocator type that obtains memory from your shared memory area, and use an instance of that allocator type instead of the default allocator when creating your container.
You may - depending on the workings of the shared memory - also need to use some synchronisation mechanism, such as a mutex, to prevent (say) instances where one application modifies the memory while another is attempting to read or modify it.