用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


好的,下面是使用C++和Boost库创建子进程并使用共享内存的示例代码: ```c++ #include <iostream> #include <boost/interprocess/shared_memory_object.hpp> #include <boost/interprocess/mapped_region.hpp> #include <boost/interprocess/ipc/message_queue.hpp> #include <boost/interprocess/sync/interprocess_mutex.hpp> #include <boost/interprocess/sync/scoped_lock.hpp> #include <boost/interprocess/sync/named_mutex.hpp> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/exceptions.hpp> #include <boost/interprocess/ipc/message_queue.hpp> #include <boost/interprocess/streams/bufferstream.hpp> #include <boost/interprocess/streams/vectorstream.hpp> #include <boost/interprocess/ipc/message_queue.hpp> #include <boost/interprocess/sync/interprocess_semaphore.hpp> #include <boost/interprocess/sync/scoped_semaphore.hpp> #include <boost/interprocess/sync/interprocess_condition.hpp> #include <boost/interprocess/sync/scoped_lock.hpp> #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp> #include <boost/process.hpp> using namespace boost::interprocess; using namespace boost::process; int main() { //创建共享内存 shared_memory_object shm_obj(create_only, "my_shm", read_write); shm_obj.truncate(1024); //设置共享内存大小为1024字节 //映射共享内存到当前进程地址空间 mapped_region shm_region(shm_obj, read_write); //获取共享内存的地址 void* shm_addr = shm_region.get_address(); //创建子进程 child c("path/to/my/child/process"); //在子进程中操作共享内存 if (c.id() == 0) { //映射共享内存到子进程地址空间 managed_shared_memory shm(open_only, "my_shm"); //获取共享内存中的vector对象 typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator; typedef vector<int, ShmemAllocator> MyVector; ShmemAllocator alloc(shm.get_segment_manager()); MyVector* myvector = shm.find_or_construct<MyVector>("MyVector")(alloc); //向vector中添加数据 myvector->push_back(1); myvector->push_back(2); myvector->push_back(3); //等待主进程读取vector中的数据 interprocess_semaphore sem(0); sem.wait(); } else //在主进程中操作共享内存 { //映射共享内存到主进程地址空间 managed_shared_memory shm(open_only, "my_shm"); //获取共享内存中的vector对象 typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator; typedef vector<int, ShmemAllocator> MyVector; ShmemAllocator alloc(shm.get_segment_manager()); MyVector* myvector = shm.find_or_construct<MyVector>("MyVector")(alloc); //读取vector中的数据并输出 for (auto it = myvector->begin(); it != myvector->end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; //释放共享内存 shared_memory_object::remove("my_shm"); //通知子进程可以退出了 interprocess_semaphore sem(1); sem.post(); } return 0; } ``` 以上代码中使用了Boost库中的`shared_memory_object`、`mapped_region`、`managed_shared_memory`、`allocator`、`vector`、`interprocess_semaphore`等类和函数,分别用于创建和操作共享内存、映射共享内存到进程地址空间、在共享内存中创建对象、向共享内存中添加数据、从共享内存中读取数据、等待和通知进程之间的同步操作等。其中,创建子进程使用了Boost.Process库中的`child`类和构造函数,可以方便地创建和管理子进程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值