Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to construct vector of vector in boost interprocess

Tags:

c++

boost

I'm new boost interprocess, and I've read the quick guide on Creating vectors in shared memory. But this example only construct a vector<int>, in my use case I have to construct more complicated data structures (usually nested containers).

Let's take an example for vector<vector<int>>, I write a small example based on that quick guide

#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;
};

However this code will give complie error:

/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()

I suppose emplace_back should work like stl vector....

like image 296
Ziqi Liu Avatar asked May 09 '26 10:05

Ziqi Liu


1 Answers

It is not as simple as you may think - you initialize the outer std::vector with an allocator. But when using emplace_back to create internal std::vector<int> compiler tries to construct it using default constructor (which does not exist due to using shared memory allocator). I see two possible solutions.

Pass allocator explicitly

Instead of

myvector->emplace_back();

use

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

for every operation adding anything to the vector. Yet, this is pretty error prone, cumbersome and there is already a known solution to this issue called:

scoped_allocator_adaptor

Just replace:

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

with:

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

and it should work fine

like image 159
bartop Avatar answered May 11 '26 05:05

bartop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!