Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. auto &data = Shared::locate(smt);
  2.  
  3. #include <cv.h>
  4. #include <cxcore.h>
  5. #include <highgui.h>
  6. #include <boost/interprocess/managed_shared_memory.hpp>
  7. #include <boost/interprocess/managed_mapped_file.hpp> // use for Coliru
  8. #include <boost/interprocess/containers/vector.hpp> // boost/containers/vector.hpp
  9. #include <boost/interprocess/containers/string.hpp> // boost/containers/string.hpp
  10. #include <iostream>
  11. #include <sys/time.h>
  12. #include <stdio.h>
  13.  
  14. // void_allocator;
  15. namespace bip = boost::interprocess;
  16.  
  17. typedef unsigned char uchar;
  18. //Typedefs of allocators and containers
  19. typedef bip::managed_shared_memory::segment_manager segment_manager_t;
  20. typedef bip::allocator<void, segment_manager_t> void_allocator;
  21.  
  22. typedef void_allocator::rebind<uchar>::other uchar_allocator;
  23. typedef bip::vector<uchar, uchar_allocator> uchar_vector;
  24.  
  25.  
  26.  
  27. template <typename Alloc = std::allocator<uchar> >
  28. struct BasicInData {
  29.  
  30. public:
  31. BasicInData(Alloc alloc = {}) : image(alloc)
  32. { }
  33.  
  34. template <typename T>
  35. BasicInData(double x, int sizeImg, uchar_vector& image, Alloc alloc = {}) :
  36. x(x), sizeImg(sizeImg), image(alloc)
  37. { }
  38.  
  39. double x = 0;
  40. int sizeImg = 0;
  41. uchar_vector image;
  42. };
  43.  
  44. using InData = BasicInData<>; // just heap allocated
  45.  
  46. namespace Shared {
  47. using segment = bip::managed_shared_memory;
  48. using segment_manager = segment::segment_manager;
  49.  
  50. template <typename T> using alloc = bip::allocator<T, segment_manager>;
  51. template <typename T> using vector = bip::vector<T, alloc<T> >;
  52.  
  53. using InData = BasicInData<alloc<uchar> >; // shared memory version
  54.  
  55. vector<InData>& locate(segment& smt) {
  56. auto* v = smt.find_or_construct<vector<InData> >("InDataVector")(smt.get_segment_manager());
  57. assert(v);
  58. return *v;
  59. }
  60. }
  61.  
  62.  
  63. int main(int argc, char* argv[]) {
  64.  
  65. if(argc == 1){ //Parent process
  66. // Remove shared memory on construction and destruction
  67. bip::shared_memory_object::remove("MySharedMemory");
  68. // Create a new segment with given name and size
  69. struct timeval tv;
  70. gettimeofday(&tv, NULL);
  71. struct shm_remove
  72. {
  73. shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
  74. ~shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
  75. }remover;
  76. Shared::segment smt(bip::create_only,"MySharedMemory", 100000000);
  77. auto &data = Shared::locate(smt);
  78. //Shared::alloc bip::alloc_inst (data);
  79.  
  80. cv::Mat_<cv::Vec3b> mat;
  81. cv::VideoCapture vcap(0);
  82.  
  83. Shared::InData id(smt.get_segment_manager());
  84.  
  85.  
  86. if (!vcap.isOpened())
  87. return -1;
  88. char count = 0;
  89. while (1) {
  90. vcap >> mat;
  91. printf ("count: %d n", count); count++;
  92. int image_size = mat.total() * mat.elemSize();
  93. printf ("size: %d n", image_size);
  94. id.sizeImg = image_size;
  95. printf ("id's sizeImg: %d n", id.sizeImg * sizeof(uchar));
  96. id.image.resize(image_size * sizeof(uchar));
  97. int totalSize = image_size * sizeof(uchar);
  98. printf ("count: %d n", count);
  99. memcpy(&id.image[0], mat.data, image_size * sizeof(uchar));
  100. //Launch child process
  101. gettimeofday(&tv, NULL);
  102. double time = ((double)tv.tv_usec/1000000);
  103. id.x = time;
  104. data.push_back(id);
  105. if((100000000 / count) <= (totalSize*20)){ printf("Getting video data done"); break; }
  106. }
  107.  
  108. std::string s(argv[0]); s += " child";
  109. if(0 != std::system(s.c_str()))
  110. return 1;
  111.  
  112. // check child has destroyed the vector
  113. if(smt.find<Shared::vector<InData>>("InDataVector").first)
  114. return 1;
  115.  
  116. } else{
  117. // Open the managed segment
  118. bip::managed_shared_memory segment(bip::open_only, "MySharedMemory");
  119.  
  120. // Find the vector using c-string name
  121. bip::vector<InData> *myvector = segment.find<bip::vector<InData>>("InDataVector").first;
  122. // Use vector in reverse order
  123.  
  124. bip::vector<InData>::iterator it;
  125.  
  126. cv::Mat_<cv::Vec3b> im;
  127. for(it = myvector->begin(); it !=myvector->end(); ++it){
  128. im.resize(it->sizeImg);
  129. memcpy(im.data, &it->image[0], it->sizeImg);
  130. cv::imshow("window1", im);
  131. }
  132.  
  133. segment.destroy<bip::vector<InData>>("InDataVector");
  134.  
  135. return 0;
  136. }
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement