共享内存中的C ++ STL映射 [英] C++ STL map in shared memory

查看:305
本文介绍了共享内存中的C ++ STL映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在共享内存中放置一个STL映射。也有多个进程访问该映射。任何指示其完成方式的指针?

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

推荐答案

要实现此目的,您需要使用一个自定义分配器,该分配器将从共享内存区域进行分配,以使映射节点全部位于共享内存中,从而使<$分配器的c $ c> pointer 类型不仅是原始指针,而且在映射到不同进程中的不同地址时可以引用共享内存区域。

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.

您还需要您的 std :: map 实现,以正确使用分配器的指针类型任何地方它都需要使用一个指针,并且不能保证按照标准

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.

目前最简单的方法是使用Boost.Interprocess,它为共享内存提供了一个不错的API,还提供了分配器和容器可以正常使用。

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.

这篇关于共享内存中的C ++ STL映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆