Boost logo

Boost Users :

Subject: [Boost-users] boost::interprocess container of pointers
From: Mateusz Ziobro (mateusz.ziobro_at_[hidden])
Date: 2009-11-22 05:59:03


Hi,
I have a problem with container of pointers in shared memory using
boost::interprocess,
object created by construct method is mapped to local memory address, so
I can not just put it into
shared memory container because the address would be invalid in other
process,
probably I have to remap or use some kind of smart pointer and than put
into container.
My question is Does someone know how to do that? Please see following
piece of code.

#include <iostream>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/set.hpp>

class TestData
{
  int value_;

public:
  TestData(int value): value_(value) { }
  int get_value() const { return value_; }
};

int main(int argc)
{
  using namespace boost::interprocess;

  managed_shared_memory mgr(open_or_create, "shm_test", 1024*1024);
  TestData* t1 = mgr.construct<TestData>("test_data")(99);

  std::cout << t1->get_value() << std::endl;

  //TestData* is address in local memory
  //we can not keep those addresses in shared memory set, would be
invalid in other process
  //typedef allocator<const TestData*,
managed_shared_memory::segment_manager> allocator;
  //typedef set<const TestData*, std::less<TestData*>, allocator>
TestDataSet;
  //TestDataSet* s1 =
mgr.construct<TestDataSet>("test_data_set")(std::less<TestData*>(),
  //
                     mgr.get_segment_manager());
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net