Reputation: 423
I have 2 application. One writes high frequency data to boost managed shared memory, data struct being boost deque.
typedef boost::interprocess::allocator<REALTIME_INFO, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef boost::interprocess::deque<REALTIME_INFO, ShmemAllocator> MyDeque;
boost::interprocess::managed_shared_memory segment(boost::interprocess::open_or_create, "MySharedMemory",50000000);
const ShmemAllocator alloc_inst (segment.get_segment_manager());
MyDeque *Mydeque;
Mydeque = segment.find_or_construct<MyDeque>("myd")(alloc_inst);//first ctor parameter
if(Mydeque->size() < 150000){
Mydeque->push_back(rtinfo);
}
else{
Mydeque->pop_front();
Mydeque->push_back(rtinfo);
}
My second application is a Qt application when pressed button reads from the shared memory and writes to a csv file. Since the data is huge I cannot write the data directly from shared memory so I tried memcpy. I could only get the first value and remaining were garbage.
managed_shared_memory segment (open_only, "MySharedMemory");
MyDeque *Mydeque = segment.find<MyDeque>("myd").first;
After getting the first pointer I tried to copy it to another MyDeque
but I can only access the first pointer and cannot iterate remaining data.
memcpy(Mydeque_new, &Mydeque, Mydeque->size()*sizeof(REALTIME_INFO));
Can anyone suggest a better way to copy data from shared memory to a local memory.
Upvotes: 2
Views: 720
Reputation: 392999
memcpy(Mydeque_new, &Mydeque, Mydeque->size()*sizeof(REALTIME_INFO));
This is big fat Undefined Behaviour because deque<>
is not a POD type. In fact, even the elements data is not contiguous in memory, so you can't even memcpy that.
Consider using a Boost Interprocess Message Queue
If there's a single consumer and a single producer, consider using spsc_queue
: Shared-memory IPC synchronization (lock-free). This is a potentially lock-less solution and it has a bulk-dequeue/enqueue interface as well.
Upvotes: 2