如何在boost进程中构造向量的向量

时间:2019-07-22 17:32:57

标签: c++ boost

我是新的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;
};

但是此代码将产生compli错误:

/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 vector一样工作。...

1 个答案:

答案 0 :(得分:1)

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

明确地通过分配器

代替

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;

它应该可以正常工作