doc_cont.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_cont
  13. #include <boost/interprocess/containers/vector.hpp>
  14. #include <boost/interprocess/allocators/allocator.hpp>
  15. #include <boost/interprocess/managed_shared_memory.hpp>
  16. //<-
  17. #include "../test/get_process_id_name.hpp"
  18. //->
  19. int main ()
  20. {
  21. using namespace boost::interprocess;
  22. //Remove shared memory on construction and destruction
  23. struct shm_remove
  24. {
  25. //<-
  26. #if 1
  27. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  28. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  29. #else
  30. //->
  31. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  32. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  33. //<-
  34. #endif
  35. //->
  36. } remover;
  37. //<-
  38. (void)remover;
  39. //->
  40. //A managed shared memory where we can construct objects
  41. //associated with a c-string
  42. //<-
  43. #if 1
  44. managed_shared_memory segment(create_only,test::get_process_id_name(), 65536);
  45. #else
  46. //->
  47. managed_shared_memory segment(create_only,
  48. "MySharedMemory", //segment name
  49. 65536);
  50. //<-
  51. #endif
  52. //->
  53. //Alias an STL-like allocator of ints that allocates ints from the segment
  54. typedef allocator<int, managed_shared_memory::segment_manager>
  55. ShmemAllocator;
  56. //Alias a vector that uses the previous STL-like allocator
  57. typedef vector<int, ShmemAllocator> MyVector;
  58. int initVal[] = {0, 1, 2, 3, 4, 5, 6 };
  59. const int *begVal = initVal;
  60. const int *endVal = initVal + sizeof(initVal)/sizeof(initVal[0]);
  61. //Initialize the STL-like allocator
  62. const ShmemAllocator alloc_inst (segment.get_segment_manager());
  63. //Construct the vector in the shared memory segment with the STL-like allocator
  64. //from a range of iterators
  65. MyVector *myvector =
  66. segment.construct<MyVector>
  67. ("MyVector")/*object name*/
  68. (begVal /*first ctor parameter*/,
  69. endVal /*second ctor parameter*/,
  70. alloc_inst /*third ctor parameter*/);
  71. //Use vector as your want
  72. std::sort(myvector->rbegin(), myvector->rend());
  73. // . . .
  74. //When done, destroy and delete vector from the segment
  75. segment.destroy<MyVector>("MyVector");
  76. return 0;
  77. }
  78. //]
  79. #include <boost/interprocess/detail/config_end.hpp>