doc_spawn_vector.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  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)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/interprocess/detail/config_begin.hpp>
  11. #include <boost/interprocess/detail/workaround.hpp>
  12. //[doc_spawn_vector
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/containers/vector.hpp>
  15. #include <boost/interprocess/allocators/allocator.hpp>
  16. #include <string>
  17. #include <cstdlib> //std::system
  18. //<-
  19. #include "../test/get_process_id_name.hpp"
  20. //->
  21. using namespace boost::interprocess;
  22. //Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
  23. //This allocator will allow placing containers in the segment
  24. typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
  25. //Alias a vector that uses the previous STL-like allocator so that allocates
  26. //its values from the segment
  27. typedef vector<int, ShmemAllocator> MyVector;
  28. //Main function. For parent process argc == 1, for child process argc == 2
  29. int main(int argc, char *argv[])
  30. {
  31. if(argc == 1){ //Parent process
  32. //Remove shared memory on construction and destruction
  33. struct shm_remove
  34. {
  35. //<-
  36. #if 1
  37. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  38. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  39. #else
  40. //->
  41. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  42. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  43. //<-
  44. #endif
  45. //->
  46. } remover;
  47. //<-
  48. (void)remover;
  49. //->
  50. //Create a new segment with given name and size
  51. //<-
  52. #if 1
  53. managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
  54. #else
  55. //->
  56. managed_shared_memory segment(create_only, "MySharedMemory", 65536);
  57. //<-
  58. #endif
  59. //->
  60. //Initialize shared memory STL-compatible allocator
  61. const ShmemAllocator alloc_inst (segment.get_segment_manager());
  62. //Construct a vector named "MyVector" in shared memory with argument alloc_inst
  63. MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
  64. for(int i = 0; i < 100; ++i) //Insert data in the vector
  65. myvector->push_back(i);
  66. //Launch child process
  67. std::string s(argv[0]); s += " child ";
  68. //<-
  69. s += test::get_process_id_name();
  70. //->
  71. if(0 != std::system(s.c_str()))
  72. return 1;
  73. //Check child has destroyed the vector
  74. if(segment.find<MyVector>("MyVector").first)
  75. return 1;
  76. }
  77. else{ //Child process
  78. //Open the managed segment
  79. //<-
  80. #if 1
  81. managed_shared_memory segment(open_only, argv[2]);
  82. #else
  83. //->
  84. managed_shared_memory segment(open_only, "MySharedMemory");
  85. //<-
  86. #endif
  87. //->
  88. //Find the vector using the c-string name
  89. MyVector *myvector = segment.find<MyVector>("MyVector").first;
  90. //Use vector in reverse order
  91. std::sort(myvector->rbegin(), myvector->rend());
  92. //When done, destroy the vector from the segment
  93. segment.destroy<MyVector>("MyVector");
  94. }
  95. return 0;
  96. }
  97. //]
  98. #include <boost/interprocess/detail/config_end.hpp>