How to construct vector in the vector by boost::interprocess
我已经学习了有关"在shared_memory中创建向量"的增强示例。
现在我的数据结构就像:
数据结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | enum FuncIndex { enmFunc_glBegin, ... } class CGLParam {}; class Funcall { vector<CGLParam> vecParams; }; class Global_Funcall { typedef allocator<CGLParam*, managed_shared_memory::segment_manager> ShmemAllocator; typedef vector<CGLParam*, ShmemAllocator> MyVector; MyVector<FunCall> vecFuncalls; }; Global_Funcall() { shared_memory_object::remove("MySharedMemory"); managed_shared_memory segment(create_only,"MySharedMemory", 65536); //Initialize shared memory STL-compatible allocator const ShmemAllocator alloc_inst(segment.get_segment_manager()); //Construct a vector named"MyVector" in shared memory with argument alloc_inst vecFuncalls= segment.construct<MyVector>("MyVector")(alloc_inst); } void InvokeFuncs(CGLParam *presult) { managed_shared_memory open_segment(open_only,"MySharedMemory"); listParams = open_segment.find<MyVector>("MyVector").first; // MyVector::const_iterator it; // for (it = listParams->cbegin(); it != listParams->cend(); it++) // { // (*it)->InvokeFunc(presult); // } } |
我的问题是"如何构造vecParams以及如何获得它"。数据量很大(opengl函数调用)
该结构用于保存opengl函数调用。
除了"显而易见的"错别字,您还尝试在
除此之外,似乎还有一个概念上的问题:
- 如果目标是拥有比物理内存更大的数据收集,那么共享内存本身将无济于事。您想查看内存映射文件
- 如果您希望共享内存是因为您可以在进程之间共享它(因此称为Boost Interprocess),那么您将需要考虑流程同步,或者由于数据竞争而看到复杂的错误。
-
您不能将原始指针安全地存储在此容器中。相反,将实际元素存储在此处(或者,如果您真的想花哨的话,可以查看
bip::offset_ptr<> )。
这是"固定"演示
- 解决C编译问题,
-
将元素类型更改为
CGLParam 而不是CGLParam* - 固定成员类型以匹配SHM向量,并
- 添加基本??的共享互斥锁同步(这本身就是一门艺术,您将需要阅读更多关于此的信息)
在Coliru上实时观看[1]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <vector> #include <boost/interprocess/managed_mapped_file.hpp> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/sync/named_mutex.hpp> #include <boost/interprocess/sync/named_recursive_mutex.hpp> #include <boost/interprocess/sync/scoped_lock.hpp> namespace bip = boost::interprocess; using mutex_type = bip::named_mutex; class CGLParam {}; typedef bip::allocator<CGLParam, bip::managed_shared_memory::segment_manager> ShmemAllocator; typedef std::vector<CGLParam, ShmemAllocator> MyVector; class Funcall { std::vector<CGLParam> vecParams; }; struct mutex_remove { mutex_remove() { mutex_type::remove("2faa9c3f-4cc0-49c5-8f79-f99ce5a5d526"); } ~mutex_remove(){ mutex_type::remove("2faa9c3f-4cc0-49c5-8f79-f99ce5a5d526"); } } remover; static mutex_type mutex(bip::open_or_create,"2faa9c3f-4cc0-49c5-8f79-f99ce5a5d526"); class Global_Funcall { MyVector* vecFuncalls; Global_Funcall() { bip::scoped_lock<mutex_type> lock(mutex); bip::shared_memory_object::remove("MySharedMemory"); bip::managed_shared_memory segment(bip::create_only,"MySharedMemory", 65536); //Initialize shared memory STL-compatible allocator const ShmemAllocator alloc_inst(segment.get_segment_manager()); //Construct a vector named"MyVector" in shared memory with argument alloc_inst vecFuncalls = segment.construct<MyVector>("MyVector")(alloc_inst); } }; void InvokeFuncs(CGLParam *presult) { bip::scoped_lock<mutex_type> lock(mutex); bip::managed_shared_memory open_segment(bip::open_only,"MySharedMemory"); auto listParams = open_segment.find<MyVector>("MyVector").first; MyVector::const_iterator it; for (it = listParams->cbegin(); it != listParams->cend(); it++) { //it->InvokeFunc(presult); } } int main() { } |
[1]当然,除非有适当的转换,否则
[2] Coliru不支持必需的IPC机制:/