C++ STL map in shared memory

允我心安 提交于 2020-01-03 04:47:32

问题


i need to place a STL map in shared memory. Also have multiple process access that map. Any pointers to how it is done ?

I have checked this link. But need a more simpler method. Map in Shared memory


回答1:


For this to work you need to use a custom allocator that will allocate from the shared memory region, so that the map nodes are all in shared memory, and so that the pointer type of the allocator is not just a raw pointer but can refer to the shared memory region when it is mapped to different addresses in different processes.

You also need your std::map implementation to correctly use the allocator's pointer type everywhere it needs to use a pointer, and this isn't guaranteed by the standard.

The simplest way to do this currently is to use Boost.Interprocess which provides a nice API for shared memory and also provides allocators and containers that work correctly with it.



来源:https://stackoverflow.com/questions/35222065/c-stl-map-in-shared-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!