【问题标题】:how to construct vector of vector in boost interprocess如何在boost进程间构造向量的向量
【发布时间】:2019-07-22 17:32:57
【问题描述】:

我是新的 boost 进程间程序,我已阅读 Creating vectors in shared memory 上的快速指南。但是这个例子只构造了一个vector<int>,在我的用例中我必须构造更复杂的数据结构(通常是嵌套容器)。

我们以vector<vector<int>>为例,我根据那个快速指南写了一个小例子

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
#include <cstdlib> //std::system
#include <iostream>

using namespace boost::interprocess;

//Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;
typedef allocator<vector<int , ShmemAllocator>, managed_shared_memory::segment_manager>  ShmemAllocator2D;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef vector<int, ShmemAllocator> MyVector;
typedef vector<MyVector, ShmemAllocator2D> My2DVector;

//Main function. For parent process argc == 1, for child process argc == 2
int main(int argc, char *argv[])
{
  if(argc == 1){ //Parent process
    //Remove shared memory on construction and destruction
    struct shm_remove
    {
      shm_remove() { shared_memory_object::remove("MySharedMemory"); }
      ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
    } remover;

    //Create a new segment with given name and size
    managed_shared_memory segment(create_only, "MySharedMemory", 65536);

    //Initialize shared memory STL-compatible allocator
    const ShmemAllocator2D alloc_inst (segment.get_segment_manager());

    //Construct a vector named "MyVector" in shared memory with argument alloc_inst
    My2DVector *myvector = segment.construct<My2DVector>("MyVector")(alloc_inst);

    for (int i = 0; i < 10;++i) {
      myvector->emplace_back();
//      for (int j = i; j < 10;++j) {
//        myvector->back().push_back(j);
//      }
    }

    //Launch child process
    std::string s(argv[0]); s += " child ";
    if(0 != std::system(s.c_str()))
      return 1;

    //Check child has destroyed the vector
    if(segment.find<MyVector>("MyVector").first)
      return 1;
  }
  else{ //Child process
    //Open the managed segment
    managed_shared_memory segment(open_only, "MySharedMemory");

    //Find the vector using the c-string name
    My2DVector *myvector = segment.find<My2DVector>("MyVector").first;

    //Use vector in reverse order
    for (auto &vec : *myvector){
      std::cout << "row" << std::endl;
      for (auto i:vec)
        std::cout << i << ", ";
      std::cout << std::endl;
    }


    //When done, destroy the vector from the segment
    segment.destroy<MyVector>("MyVector");
  }

  return 0;
};

但是这段代码会给出编译错误:

/usr/include/boost/container/vector.hpp:301:54: error: no matching function for call to ‘boost::interprocess::allocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()’
       : Allocator(), m_start(), m_size(), m_capacity()

我想emplace_back 应该像 stl 矢量一样工作......

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    这并不像您想象的那么简单 - 您使用分配器初始化外部 std::vector。但是当使用emplace_back 创建内部std::vector&lt;int&gt; 编译器尝试使用默认构造函数(由于使用共享内存分配器而不存在)来构造它。我看到了两种可能的解决方案。

    显式传递分配器

    代替

    myvector->emplace_back();
    

    使用

    ShmemAllocator2D ac(segment.get_segment_manager());
    myvector->emplace_back(ac);
    

    对于向向量添加任何内容的每个操作。然而,这很容易出错,很麻烦,并且已经有一个已知的解决方案可以解决这个问题:

    scoped_allocator_adaptor

    只需替换:

    typedef allocator<vector<int , ShmemAllocator>, managed_shared_memory::segment_manager>  ShmemAllocator2D;
    

    与:

    typedef scoped_allocator_adaptor<allocator<vector<int , ShmemAllocator>, managed_shared_memory::segment_manager>>  ShmemAllocator2D;
    

    它应该可以正常工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 2014-12-06
      • 2017-11-18
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      相关资源
      最近更新 更多