问题
Trying to implement a produced consumer scenario where one process feeds cv::Mat objects into a queue buffer. And the consumer consumes them. cv::Mat has a settable allocator that can be implemented for custom memory management, but I had no success in making it work. Popping from the que on consumer side led to segfaults. The closest I've got is this implementation whwre cv::Mat is serialized and deserialized. Another downside of this implementation is buffer size is defined during compilation. So to reiterate the questions: how to efficiently implement cv::Mat lockfree queue in a shared memory.
Related questions:
- unable to construct runtime boost spsc_queue with runtime size parameter in shared memory
- Shared-memory IPC synchronization (lock-free)
- Boost shared memory and synchronized queue issue/crash in consumer process
回答1:
The "settable" allocator for cv::Mat
is NOT a Boost Interprocess allocator.
It looks like it's gonna be "hard" to implement the cv::Matallocator interface to wrap one, as well.
This could be because the fancier allocators are intended for CUDA support, but I'm guessing a bit here.
So, I'd strongly suggest serializing. This should be okay unless you're dealing with giant matrices. See e.g.
- Serialization of cv::Mat giving strange result
Of course you can serialize to shared memory: https://www.boost.org/doc/libs/1_37_0/doc/html/interprocess/streams.html or https://www.boost.org/doc/libs/1_74_0/libs/iostreams/doc/quick_reference.html#devices
Now if you need large matrices (and they NEED to be OpenCV anyways) consider using existing CV allocators to allocate from an already existing contiguous buffer in your shared memory.
This could be as simple as just a vector<int8_t, bip::allocator<int8_t> >
or, indeed array<int8_t, 4096>
constructed inside shared memory (either managed (managed_shared_memory
) or unmanaged (bip::mapped_region
that works on top of bip::shared_memory_object
).
来源:https://stackoverflow.com/questions/64897387/how-to-construct-boost-spsc-queue-with-runtime-size-parameter-to-exchange-cvma