#include #include #include #include #include #include using namespace boost::interprocess; struct Foo { Foo( int _x ) : x(_x) {} int x; }; typedef managed_shared_ptr::type FooSharedPtrType; typedef managed_weak_ptr ::type FooWeakPtrType; typedef allocator FooWeakPtrAllocatorType; typedef allocator FooSharedPtrAllocatorType; typedef vector FooWeakPtrVectorType; typedef vector FooSharedPtrVectorType; struct FooWeakPtrOp{ void operator()( const FooWeakPtrType & a ) { std::cout << a.lock()->x << "\n"; } }; struct FooSharedPtrOp{ void operator()( const FooSharedPtrType & a ){ std::cout << a->x << "\n"; } }; int main() { //Remove existing segment shared_memory_object::remove("MySharedMemory"); //Create a new segment with given name and size fixed_managed_shared_memory segment(create_only, "MySharedMemory", 65536); //Initialize shared memory STL-compatible allocator const FooWeakPtrAllocatorType alloc_inst_weak (segment.get_segment_manager()); const FooSharedPtrAllocatorType alloc_inst_shared (segment.get_segment_manager()); //Construct a vector named "MyVector" in shared memory with argument alloc_inst FooWeakPtrVectorType *foo_weak_vector = segment.construct("MyWeakVector")(alloc_inst_weak); FooSharedPtrVectorType *foo_shared_vector = segment.construct("MySharedVector")(alloc_inst_shared); FooSharedPtrType foo_ptr1 = make_managed_shared_ptr(segment.construct(anonymous_instance)(1), segment); FooSharedPtrType foo_ptr2 = make_managed_shared_ptr(segment.construct(anonymous_instance)(2), segment); FooSharedPtrType foo_ptr3 = make_managed_shared_ptr(segment.construct(anonymous_instance)(3), segment); foo_weak_vector->push_back(foo_ptr1); foo_weak_vector->push_back(foo_ptr2); foo_weak_vector->push_back(foo_ptr3); foo_shared_vector->push_back(foo_ptr1); foo_shared_vector->push_back(foo_ptr2); foo_shared_vector->push_back(foo_ptr3); foo_weak_vector->erase(foo_weak_vector->begin()+1); foo_shared_vector->erase(foo_shared_vector->begin()+1); std::cout << "foo_shared_vector:" << std::endl; std::for_each( foo_shared_vector->begin(), foo_shared_vector->end(), FooSharedPtrOp() ); std::cout << "foo_weak_vector:" << std::endl; std::for_each( foo_weak_vector->begin(), foo_weak_vector->end(), FooWeakPtrOp() ); }