[关闭]
@Pigmon 2019-01-07T15:47:59.000000Z 字数 5155 阅读 78

PointCound2 的 boost::interprocess 容器及其共享内存的条件同步存取

C++


PointCound2 的 boost::interprocess 容器

  1. #pragma once
  2. #include <string>
  3. #include <boost/interprocess/managed_shared_memory.hpp>
  4. #include <boost/interprocess/managed_shared_memory.hpp>
  5. #include <boost/interprocess/allocators/allocator.hpp>
  6. #include <boost/interprocess/containers/vector.hpp>
  7. #include <boost/interprocess/containers/string.hpp>
  8. using namespace boost::interprocess;
  9. typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
  10. typedef basic_string<char, std::char_traits<char>, CharAllocator> AString;
  11. struct H_PCLHeader
  12. {
  13. unsigned int seq;
  14. unsigned long stamp;
  15. AString* frame_id;
  16. H_PCLHeader() : seq(0), stamp()
  17. {
  18. }
  19. };
  20. typedef allocator<H_PCLHeader, managed_shared_memory::segment_manager> Header_Allocator;
  21. struct H_PCLPointField
  22. {
  23. H_PCLPointField() : name(), offset(0), datatype(0), count(0)
  24. {
  25. }
  26. std::string name;
  27. unsigned int offset;
  28. unsigned char datatype;
  29. unsigned int count;
  30. };
  31. typedef allocator<H_PCLPointField, managed_shared_memory::segment_manager> Field_Allocator;
  32. typedef vector<H_PCLPointField, Field_Allocator> FieldVec;
  33. typedef allocator<unsigned char, managed_shared_memory::segment_manager> Unit_Allocator;
  34. typedef vector<unsigned char, Unit_Allocator> DataVec;
  35. struct H_PCLPointCloud2
  36. {
  37. H_PCLHeader *header;
  38. unsigned int height;
  39. unsigned int width;
  40. FieldVec *fields;
  41. unsigned char is_bigendian;
  42. unsigned int point_step;
  43. unsigned int row_step;
  44. DataVec *data;
  45. unsigned char is_dense;
  46. H_PCLPointCloud2() : height(10), width(0), is_bigendian(false), point_step(0), row_step(0), is_dense(false)
  47. {
  48. #if defined(BOOST_BIG_ENDIAN)
  49. is_bigendian = true;
  50. #elif defined(BOOST_LITTLE_ENDIAN)
  51. is_bigendian = false;
  52. #endif
  53. }
  54. };

模拟的生产者

  1. #include <boost/interprocess/managed_shared_memory.hpp>
  2. #include <boost/interprocess/containers/vector.hpp>
  3. #include <boost/interprocess/allocators/allocator.hpp>
  4. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  5. #include <boost/interprocess/sync/interprocess_condition.hpp>
  6. #include <boost/interprocess/sync/scoped_lock.hpp>
  7. #include <string>
  8. //#include <cstdlib> //std::system
  9. #include <iostream>
  10. #include "pc2.h"
  11. using namespace boost::interprocess;
  12. typedef allocator<H_PCLPointCloud2, managed_shared_memory::segment_manager> Pc2_Allocator;
  13. int main(int argc, char *argv[])
  14. {
  15. shared_memory_object::remove("MySharedMemory");
  16. managed_shared_memory segment(create_only, "MySharedMemory", 65536);
  17. interprocess_mutex *mtx = segment.find_or_construct<interprocess_mutex>("mtx")();
  18. interprocess_condition *cnd = segment.find_or_construct<interprocess_condition>("cnd")();
  19. scoped_lock<interprocess_mutex> lock{*mtx};
  20. H_PCLPointCloud2 *pc2 = segment.construct<H_PCLPointCloud2>("pc2")();
  21. H_PCLHeader *header = segment.construct<H_PCLHeader>("header")();
  22. AString *frame_id = segment.construct<AString>("frame_id")("velodyne", segment.get_segment_manager());
  23. const Field_Allocator field_alloc_inst(segment.get_segment_manager());
  24. FieldVec *fields = (segment.construct<FieldVec>("fields")(field_alloc_inst));
  25. const Unit_Allocator unit_alloc_inst(segment.get_segment_manager());
  26. DataVec *data = segment.construct<DataVec>("data")(unit_alloc_inst);
  27. int counter = 0;
  28. while (counter < 10)
  29. {
  30. counter++;
  31. std::cout << counter << " 1" << std::endl;
  32. pc2->height = 20 + counter;
  33. std::cout << counter << " 2" << std::endl;
  34. header->seq = counter;
  35. header->stamp = counter * 10;
  36. pc2->header = header;
  37. std::cout << counter << " 3" << std::endl;
  38. fields->clear();
  39. H_PCLPointField field1, field2;
  40. field1.count = counter + 1;
  41. field2.count = counter + 2;
  42. fields->push_back(field1);
  43. fields->push_back(field2);
  44. pc2->fields = fields;
  45. std::cout << counter << " 4" << std::endl;
  46. data->clear();
  47. data->push_back(100 * counter);
  48. data->push_back(101 * counter);
  49. data->push_back(102 * counter);
  50. pc2->data = data;
  51. cnd->notify_all();
  52. cnd->wait(lock);
  53. }
  54. cnd->notify_all();
  55. shared_memory_object::remove("MySharedMemory");
  56. return 0;
  57. }

模拟的消费者

  1. #include <boost/interprocess/managed_shared_memory.hpp>
  2. #include <boost/interprocess/containers/vector.hpp>
  3. #include <boost/interprocess/allocators/allocator.hpp>
  4. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  5. #include <boost/interprocess/sync/interprocess_condition.hpp>
  6. #include <boost/interprocess/sync/scoped_lock.hpp>
  7. #include <string>
  8. #include <iostream>
  9. #include "pc2.h"
  10. using namespace boost::interprocess;
  11. int main(int argc, char *argv[])
  12. {
  13. managed_shared_memory segment(open_only, "MySharedMemory");
  14. interprocess_mutex *mtx = segment.find_or_construct<interprocess_mutex>("mtx")();
  15. interprocess_condition *cnd = segment.find_or_construct<interprocess_condition>("cnd")();
  16. scoped_lock<interprocess_mutex> lock{*mtx};
  17. H_PCLPointCloud2 *pc2 = segment.find<H_PCLPointCloud2>("pc2").first;
  18. int counter = 0;
  19. while(counter < 10)
  20. {
  21. counter++;
  22. if (pc2)
  23. {
  24. std::cout << pc2->height << std::endl;
  25. pc2->header = segment.find<H_PCLHeader>("header").first;
  26. if (pc2->header)
  27. {
  28. std::cout << pc2->header->seq << ", " << pc2->header->stamp << std::endl;
  29. pc2->header->frame_id = segment.find<AString>("frame_id").first;
  30. if (pc2->header->frame_id)
  31. {
  32. std::cout << *(pc2->header->frame_id) << std::endl;
  33. }
  34. }
  35. pc2->fields = segment.find<FieldVec>("fields").first;
  36. if (pc2->fields)
  37. {
  38. for (auto it = pc2->fields->begin(); it != pc2->fields->end(); it++)
  39. {
  40. std::cout << (*it).count << ",";
  41. }
  42. std::cout << std::endl;
  43. }
  44. pc2->data = segment.find<DataVec>("data").first;
  45. if (pc2->data)
  46. {
  47. for (auto it = pc2->data->begin(); it != pc2->data->end(); it++)
  48. std::cout << (unsigned int)(*it) << ", ";
  49. std::cout << std::endl;
  50. }
  51. }
  52. std::cout << std::endl;
  53. cnd->notify_all();
  54. cnd->wait(lock);
  55. }
  56. cnd->notify_all();
  57. return 0;
  58. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注