1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2009. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <cstdlib> //std::system
19 #include "../test/get_process_id_name.hpp"
22 int main(int argc, char *argv[])
24 using namespace boost::interprocess;
25 typedef std::pair<double, int> MyType;
27 if(argc == 1){ //Parent process
28 //Remove shared memory on construction and destruction
33 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
34 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
37 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
38 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
44 //Construct managed shared memory
47 managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
50 managed_shared_memory segment(create_only, "MySharedMemory", 65536);
55 //Create an object of MyType initialized to {0.0, 0}
56 MyType *instance = segment.construct<MyType>
57 ("MyType instance") //name of the object
58 (0.0, 0); //ctor first argument
60 //Create an array of 10 elements of MyType initialized to {0.0, 0}
61 MyType *array = segment.construct<MyType>
62 ("MyType array") //name of the object
63 [10] //number of elements
64 (0.0, 0); //Same two ctor arguments for all objects
66 //Create an array of 3 elements of MyType initializing each one
67 //to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}...
68 float float_initializer[3] = { 0.0, 1.0, 2.0 };
69 int int_initializer[3] = { 0, 1, 2 };
71 MyType *array_it = segment.construct_it<MyType>
72 ("MyType array from it") //name of the object
73 [3] //number of elements
74 ( &float_initializer[0] //Iterator for the 1st ctor argument
75 , &int_initializer[0]); //Iterator for the 2nd ctor argument
77 //Launch child process
78 std::string s(argv[0]); s += " child ";
80 s += test::get_process_id_name();
82 if(0 != std::system(s.c_str()))
91 //Check child has destroyed all objects
92 if(segment.find<MyType>("MyType array").first ||
93 segment.find<MyType>("MyType instance").first ||
94 segment.find<MyType>("MyType array from it").first)
98 //Open managed shared memory
101 managed_shared_memory segment(open_only, argv[2]);
104 managed_shared_memory segment(open_only, "MySharedMemory");
109 std::pair<MyType*, std::size_t> res;
112 res = segment.find<MyType> ("MyType array");
113 //Length should be 10
114 if(res.second != 10) return 1;
117 res = segment.find<MyType> ("MyType instance");
119 if(res.second != 1) return 1;
121 //Find the array constructed from iterators
122 res = segment.find<MyType> ("MyType array from it");
124 if(res.second != 3) return 1;
126 //We're done, delete all the objects
127 segment.destroy<MyType>("MyType array");
128 segment.destroy<MyType>("MyType instance");
129 segment.destroy<MyType>("MyType array from it");
134 #include <boost/interprocess/detail/config_end.hpp>