@Pigmon
2019-01-07T23:47:59.000000Z
字数 5155
阅读 1054
C++
#pragma once
#include <string>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
using namespace boost::interprocess;
typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
typedef basic_string<char, std::char_traits<char>, CharAllocator> AString;
struct H_PCLHeader
{
unsigned int seq;
unsigned long stamp;
AString* frame_id;
H_PCLHeader() : seq(0), stamp()
{
}
};
typedef allocator<H_PCLHeader, managed_shared_memory::segment_manager> Header_Allocator;
struct H_PCLPointField
{
H_PCLPointField() : name(), offset(0), datatype(0), count(0)
{
}
std::string name;
unsigned int offset;
unsigned char datatype;
unsigned int count;
};
typedef allocator<H_PCLPointField, managed_shared_memory::segment_manager> Field_Allocator;
typedef vector<H_PCLPointField, Field_Allocator> FieldVec;
typedef allocator<unsigned char, managed_shared_memory::segment_manager> Unit_Allocator;
typedef vector<unsigned char, Unit_Allocator> DataVec;
struct H_PCLPointCloud2
{
H_PCLHeader *header;
unsigned int height;
unsigned int width;
FieldVec *fields;
unsigned char is_bigendian;
unsigned int point_step;
unsigned int row_step;
DataVec *data;
unsigned char is_dense;
H_PCLPointCloud2() : height(10), width(0), is_bigendian(false), point_step(0), row_step(0), is_dense(false)
{
#if defined(BOOST_BIG_ENDIAN)
is_bigendian = true;
#elif defined(BOOST_LITTLE_ENDIAN)
is_bigendian = false;
#endif
}
};
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <string>
//#include <cstdlib> //std::system
#include <iostream>
#include "pc2.h"
using namespace boost::interprocess;
typedef allocator<H_PCLPointCloud2, managed_shared_memory::segment_manager> Pc2_Allocator;
int main(int argc, char *argv[])
{
shared_memory_object::remove("MySharedMemory");
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
interprocess_mutex *mtx = segment.find_or_construct<interprocess_mutex>("mtx")();
interprocess_condition *cnd = segment.find_or_construct<interprocess_condition>("cnd")();
scoped_lock<interprocess_mutex> lock{*mtx};
H_PCLPointCloud2 *pc2 = segment.construct<H_PCLPointCloud2>("pc2")();
H_PCLHeader *header = segment.construct<H_PCLHeader>("header")();
AString *frame_id = segment.construct<AString>("frame_id")("velodyne", segment.get_segment_manager());
const Field_Allocator field_alloc_inst(segment.get_segment_manager());
FieldVec *fields = (segment.construct<FieldVec>("fields")(field_alloc_inst));
const Unit_Allocator unit_alloc_inst(segment.get_segment_manager());
DataVec *data = segment.construct<DataVec>("data")(unit_alloc_inst);
int counter = 0;
while (counter < 10)
{
counter++;
std::cout << counter << " 1" << std::endl;
pc2->height = 20 + counter;
std::cout << counter << " 2" << std::endl;
header->seq = counter;
header->stamp = counter * 10;
pc2->header = header;
std::cout << counter << " 3" << std::endl;
fields->clear();
H_PCLPointField field1, field2;
field1.count = counter + 1;
field2.count = counter + 2;
fields->push_back(field1);
fields->push_back(field2);
pc2->fields = fields;
std::cout << counter << " 4" << std::endl;
data->clear();
data->push_back(100 * counter);
data->push_back(101 * counter);
data->push_back(102 * counter);
pc2->data = data;
cnd->notify_all();
cnd->wait(lock);
}
cnd->notify_all();
shared_memory_object::remove("MySharedMemory");
return 0;
}
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <string>
#include <iostream>
#include "pc2.h"
using namespace boost::interprocess;
int main(int argc, char *argv[])
{
managed_shared_memory segment(open_only, "MySharedMemory");
interprocess_mutex *mtx = segment.find_or_construct<interprocess_mutex>("mtx")();
interprocess_condition *cnd = segment.find_or_construct<interprocess_condition>("cnd")();
scoped_lock<interprocess_mutex> lock{*mtx};
H_PCLPointCloud2 *pc2 = segment.find<H_PCLPointCloud2>("pc2").first;
int counter = 0;
while(counter < 10)
{
counter++;
if (pc2)
{
std::cout << pc2->height << std::endl;
pc2->header = segment.find<H_PCLHeader>("header").first;
if (pc2->header)
{
std::cout << pc2->header->seq << ", " << pc2->header->stamp << std::endl;
pc2->header->frame_id = segment.find<AString>("frame_id").first;
if (pc2->header->frame_id)
{
std::cout << *(pc2->header->frame_id) << std::endl;
}
}
pc2->fields = segment.find<FieldVec>("fields").first;
if (pc2->fields)
{
for (auto it = pc2->fields->begin(); it != pc2->fields->end(); it++)
{
std::cout << (*it).count << ",";
}
std::cout << std::endl;
}
pc2->data = segment.find<DataVec>("data").first;
if (pc2->data)
{
for (auto it = pc2->data->begin(); it != pc2->data->end(); it++)
std::cout << (unsigned int)(*it) << ", ";
std::cout << std::endl;
}
}
std::cout << std::endl;
cnd->notify_all();
cnd->wait(lock);
}
cnd->notify_all();
return 0;
}