用boost在共享内存上创建一个复杂的map

boost的interprocess类提供了在共享内存上创建复杂数据对象和容器的方式,以下是在共享内存上创建一个string map的代码,代码在32位linux上测试通过
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <iostream>

using namespace boost::interprocess;

//类型和allocator的定义,使用共享内存时需要使用boost::interprocess中
//重新实现的容器而不能使用标准容器
typedef managed_shared_memory::segment_manager                       segment_manager_t;
typedef allocator<void, segment_manager_t>                           void_allocator;
typedef allocator<int, segment_manager_t>                            int_allocator;
typedef vector<int, int_allocator>                                   int_vector;
typedef allocator<int_vector, segment_manager_t>                     int_vector_allocator;
typedef vector<int_vector, int_vector_allocator>                     int_vector_vector;
typedef allocator<char, segment_manager_t>                           char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator>   char_string;

class complex_data
{
  int               id_;
  char_string       char_string_;
  int_vector_vector int_vector_vector_;

public:
  //因为void_allocator能够转换为任何类型的allocator<T>, 所以我们能够简单的在构造函数中
  //使用void_allocator来初始化所有的内部容器
  complex_data(int id, const char *name, const void_allocator &void_alloc)
    : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc)
  {}
  ~complex_data(){}
};

//将map的key定义为一个string,而把map的value定义为一个complex_data对象
typedef std::pair<const char_string, complex_data>                      map_value_type;
typedef allocator<map_value_type, segment_manager_t>                    map_value_type_allocator;
typedef map< char_string, complex_data
, std::less<char_string>, map_value_type_allocator>          complex_map_type;

int main ()
{
  //在程序开始和结束的时候移除同名的共享内存
  //如果只是读其他程序创建的共享内存块则不该包含remover
  struct shm_remove
  {
    shm_remove() { shared_memory_object::remove("MySharedMemory"); }
    ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  } remover;
  //创建共享内存,根据测试,在32位的linux上,单块命名的共享内存大小
  //最大为2^31-1,如果是读其他程序创建的共享内存,则此句应写为
  //managed_shared_memory segment(open_only, "MySharedMemory");
  managed_shared_memory segment(create_only,"MySharedMemory", 65536);

  //一个能够转换为任何allocator<T, segment_manager_t>类型的allocator 
  void_allocator alloc_inst (segment.get_segment_manager());

  //在共享内存上创建map
  //如果map已由其他程序创建,或者不确定map是否已创建,应如下:
  //complex_map_type *mymap = segment.find_or_construct<complex_map_type>
  complex_map_type *mymap = segment.construct<complex_map_type>
    ("MyMap")(std::less<char_string>(), alloc_inst);

  for(int i = 0; i < 100; ++i){
    //key(string) 和value(complex_data) 都需要在他们的构造函数中
	//包含一个allocator
	char tmp[16] = "";
	sprintf(tmp, "test%d", i);
    char_string  key_object(tmp, alloc_inst);
    complex_data mapped_object(i, "default_name", alloc_inst);
    map_value_type value(key_object, mapped_object);
    //向map中插值
    mymap->insert(value);
  }

  return 0;
}

此函数创建了一个map,其中key为string,value为一个复杂的结构,其中还包含了一个int vector

因为工程需要,只对boost参考文档中建立普通容器的容器做了实验,更复杂的容器需参照boost文档进行

此程序使用boost版本为1.42.0

编译时无需对boost进行单独编译,只需引用相应的(hpp)头文件即可使用,编译时需连接库"-lrt",如果在64位系统上编译,需要连接-lpthread


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值