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