i007.cc

i007.cc

优先队列-降维打击

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

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

boost的interprocess类提供了在共享内存上创建复杂数据对象和容器的方式,以下是在共享内存上创建一个string map的代码,代码在32位linux上测试通过

  1. #include <boost/interprocess/managed_shared_memory.hpp>
  2. #include <boost/interprocess/allocators/allocator.hpp>
  3. #include <boost/interprocess/containers/map.hpp>
  4. #include <boost/interprocess/containers/vector.hpp>
  5. #include <boost/interprocess/containers/string.hpp>
  6. #include <iostream>
  7. using namespace boost::interprocess;
  8. //类型和allocator的定义,使用共享内存时需要使用boost::interprocess中
  9. //重新实现的容器而不能使用标准容器
  10. typedef managed_shared_memory::segment_manager                       segment_manager_t;
  11. typedef allocator<void, segment_manager_t>                           void_allocator;
  12. typedef allocator<int, segment_manager_t>                            int_allocator;
  13. typedef vector<int, int_allocator>                                   int_vector;
  14. typedef allocator<int_vector, segment_manager_t>                     int_vector_allocator;
  15. typedef vector<int_vector, int_vector_allocator>                     int_vector_vector;
  16. typedef allocator<char, segment_manager_t>                           char_allocator;
  17. typedef basic_string<char, std::char_traits<char>, char_allocator>   char_string;
  18. class complex_data
  19. {
  20.   int               id_;
  21.   char_string       char_string_;
  22.   int_vector_vector int_vector_vector_;
  23. public:
  24.   //因为void_allocator能够转换为任何类型的allocator<T>, 所以我们能够简单的在构造函数中
  25.   //使用void_allocator来初始化所有的内部容器
  26.   complex_data(int id, const char *name, const void_allocator &void_alloc)
  27.     : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc)
  28.   {}
  29.   ~complex_data(){}
  30. };
  31. //将map的key定义为一个string,而把map的value定义为一个complex_data对象
  32. typedef std::pair<const char_string, complex_data>                      map_value_type;
  33. typedef allocator<map_value_type, segment_manager_t>                    map_value_type_allocator;
  34. typedef map< char_string, complex_data
  35. , std::less<char_string>, map_value_type_allocator>          complex_map_type;
  36. int main ()
  37. {
  38.   //在程序开始和结束的时候移除同名的共享内存
  39.   //如果只是读其他程序创建的共享内存块则不该包含remover
  40.   struct shm_remove
  41.   {
  42.     shm_remove() { shared_memory_object::remove(“MySharedMemory”); }
  43.     ~shm_remove(){ shared_memory_object::remove(“MySharedMemory”); }
  44.   } remover;
  45.   //创建共享内存,根据测试,在32位的linux上,单块命名的共享内存大小
  46.   //最大为2^31-1,如果是读其他程序创建的共享内存,则此句应写为
  47.   //managed_shared_memory segment(open_only, “MySharedMemory”);
  48.   managed_shared_memory segment(create_only,“MySharedMemory”, 65536);
  49.   //一个能够转换为任何allocator<T, segment_manager_t>类型的allocator 
  50.   void_allocator alloc_inst (segment.get_segment_manager());
  51.   //在共享内存上创建map
  52.   //如果map已由其他程序创建,或者不确定map是否已创建,应如下:
  53.   //complex_map_type *mymap = segment.find_or_construct<complex_map_type>
  54.   complex_map_type *mymap = segment.construct<complex_map_type>
  55.     (“MyMap”)(std::less<char_string>(), alloc_inst);
  56.   for(int i = 0; i < 100; ++i){
  57.     //key(string) 和value(complex_data) 都需要在他们的构造函数中
  58.     //包含一个allocator
  59.     char tmp[16] = “”;
  60.     sprintf(tmp, “test%d”, i);
  61.     char_string  key_object(tmp, alloc_inst);
  62.     complex_data mapped_object(i, “default_name”, alloc_inst);
  63.     map_value_type value(key_object, mapped_object);
  64.     //向map中插值
  65.     mymap->insert(value);
  66.   }
  67.   return 0;
  68. }

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

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

此程序使用boost版本为1.42.0

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

个人分类: c++

发表回复