如何使用运行时大小参数构造 boost spsc_queue 以在使用共享内存的两个进程之间交换 cv::Mat 对象?

How to construct boost spsc_queue with runtime size parameter to exchange cv::Mat objects between two processes using shared memory?

尝试实现一种生产的消费者场景,其中一个进程提供 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 被序列化和反序列化。此实现的另一个缺点是缓冲区大小是在编译期间定义的。所以重申问题:如何在共享内存中有效地实现cv::Mat无锁队列。

相关问题:

cv::Mat 的“可设置”分配器不是 Boost 进程间分配器。

看起来也“很难”实施 the cv::Matallocator interface 包装一个。

This could be because the fancier allocators are intended for CUDA support, but I'm guessing a bit here.

所以,我强烈建议连载。除非您要处理巨型矩阵,否则这应该没问题。参见示例

当然可以序列化到共享内存: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

现在,如果您需要大型矩阵(无论如何它们都需要是 OpenCV),请考虑使用现有的 CV 分配器从共享内存中的已经存在的连续缓冲区进行分配。

这可以像 vector<int8_t, bip::allocator<int8_t> > 一样简单,或者 array<int8_t, 4096> 在共享内存中构造(托管 (managed_shared_memory) 或非托管 (bip::mapped_region 有效在 bip::shared_memory_object).

之上