]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_spawn_vector.cpp
c2f0dfe777e49693b26781b848e136192d0fe6cc
1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2012. 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 <boost/interprocess/containers/vector.hpp>
15 #include <boost/interprocess/allocators/allocator.hpp>
17 #include <cstdlib> //std::system
19 #include "../test/get_process_id_name.hpp"
22 using namespace boost::interprocess
;
24 //Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
25 //This allocator will allow placing containers in the segment
26 typedef allocator
<int, managed_shared_memory::segment_manager
> ShmemAllocator
;
28 //Alias a vector that uses the previous STL-like allocator so that allocates
29 //its values from the segment
30 typedef vector
<int, ShmemAllocator
> MyVector
;
32 //Main function. For parent process argc == 1, for child process argc == 2
33 int main(int argc
, char *argv
[])
35 if(argc
== 1){ //Parent process
36 //Remove shared memory on construction and destruction
41 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
42 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
45 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
46 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
55 //Create a new segment with given name and size
58 managed_shared_memory
segment(create_only
, test::get_process_id_name(), 65536);
61 managed_shared_memory
segment(create_only
, "MySharedMemory", 65536);
66 //Initialize shared memory STL-compatible allocator
67 const ShmemAllocator
alloc_inst (segment
.get_segment_manager());
69 //Construct a vector named "MyVector" in shared memory with argument alloc_inst
70 MyVector
*myvector
= segment
.construct
<MyVector
>("MyVector")(alloc_inst
);
72 for(int i
= 0; i
< 100; ++i
) //Insert data in the vector
73 myvector
->push_back(i
);
75 //Launch child process
76 std::string
s(argv
[0]); s
+= " child ";
78 s
+= test::get_process_id_name();
80 if(0 != std::system(s
.c_str()))
83 //Check child has destroyed the vector
84 if(segment
.find
<MyVector
>("MyVector").first
)
88 //Open the managed segment
91 managed_shared_memory
segment(open_only
, argv
[2]);
94 managed_shared_memory
segment(open_only
, "MySharedMemory");
99 //Find the vector using the c-string name
100 MyVector
*myvector
= segment
.find
<MyVector
>("MyVector").first
;
102 //Use vector in reverse order
103 std::sort(myvector
->rbegin(), myvector
->rend());
105 //When done, destroy the vector from the segment
106 segment
.destroy
<MyVector
>("MyVector");
113 #include <boost/interprocess/detail/config_end.hpp>