Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/interprocess/managed_shared_memory.hpp>
- #include <cstring>
- #include <string>
- #include <cstdlib> //std::system
- #include <iostream>
- #include <stdio.h>
- using namespace boost::interprocess;
- class MyClass {
- public:
- MyClass();
- ~MyClass();
- std::string name;
- };
- MyClass::MyClass() { this->name = "init"; }
- MyClass::~MyClass() { }
- int main( int argc, char *argv[] ) {
- if ( argc == 1 ) { // Parent process
- std::cout << "parent: " << std::endl;
- struct shm_remove { // Remove shared memory on construction and destruction
- shm_remove() { shared_memory_object::remove("MySharedMemory"); }
- ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
- } remover;
- managed_shared_memory shm(create_only, "MySharedMemory", 1000); // Create a managed shared memory
- MyClass *myclass = shm.construct<MyClass>("MyClass")(); // Construct a named object
- std::cout << "parent see: " << myclass << std::endl;
- std::cout << "parent see: " << myclass->name << std::endl;
- std::string s( argv[0] );
- s += " child ";
- std::system( s.c_str() );
- } else {
- std::cout << "child: " << std::endl;
- managed_shared_memory shm(open_only, "MySharedMemory");
- MyClass *myclass = shm.find<MyClass>("MyClass").first;
- std::cout << "child see: " << myclass << std::endl;
- std::cout << "child see: " << myclass->name << std::endl;
- }
- return 0;
- }
- parent:
- parent see: 0x7f54974fc118
- parent see: init
- child:
- child see: 0x7f8431583118
- Segmentation fault
Add Comment
Please, Sign In to add comment