用boost在共享内存上创建一个复杂的map - 小众知识

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

2022-08-02 03:43:02 苏内容
  标签: boost/共享/内存
阅读:2199

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

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

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

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

此程序使用boost版本为1.42.0

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



Hi all

How to initialising boost::interprocess::basic_string using std::string;


Use this constructor passing std::string::c_str() (and don't forget the
allocator, interprocess containers need always a valid allocator to
allocate memory in shared memory):

basic_string(const CharT* s,
const allocator_type& a = allocator_type())


typedef /*...*/ interprocess_string_t;

interprocess_string_t * =
managed_shm.construct<interprocess_string_t>("name")
(std_str.c_str(), managed_shm.get_allocator<char>());

Best,

Ion


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1