2

我已经列举了关于“在shared_memory中创建向量”的boost样本。 现在我的数据结构是这样的:如何通过boost :: interprocess构建向量中的向量

数据结构:

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函数调用。

+0

看来你在C++语言中遇到了麻烦。没关系。但是我建议_不要使用C++的进程间技术,除非你必须同时学习这两个复杂的主题。 (我非常确定,例如[tag:python]或[tag:c#-4.0]将具有处理大型数据集所需的工具。) – sehe

+0

如果这仅仅是大型数据集,[STXXL](http:///stxxl.sourceforge.net/)可能对您有些用处。 – wilx

回答

1

除了“明显”错别字,你尝试在GlobalFuncall构造分配IPC矢量(MyVector*)到标准载体。这将永远不会工作。 C++是一种强类型语言,因此如果您想分配[1],则类型必须匹配。

除了这似乎是一个概念性的问题:

  • 如果目标是有一个数据集合,可能比phyical内存的容量较大,本身共享内存是不会帮。你会想在内存映射文件
  • 看,如果你想共享内存,因为你可以在进程(因此升压进程间)之间共享它,你会需要思考过程同步的,或者你由于数据竞赛会看到复杂的错误。
  • 不能安全地存储此容器内的原始指针。相反,将实际元素存储在那里(或者也许bip::offset_ptr<>如果你想变得很花哨)。

这里的一个“固定向上”示范

  • 固定C++编译问题,
  • 改变元素的类型为CGLParam代替CGLParam*
  • 固定构件类型相匹配的SHM矢量和
  • 添加基本的共享互斥锁同步(这本身就是一门艺术,您需要阅读更多关于此的内容)

查看它Live On Coliru[1]

#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没有按” t支持所需的IPC机制:/

+0

我这样做是有原因的。如果我把所有CGlParam放入一个矢量。 threr的麻烦来决定函数调用的名称和要调用的参数。 – perlinson

+0

@sehe:快速提问你的代码,'segment'变量会在GlobalFuncall结束时被销毁吗?因为它超出了范围!共享内存肯定会保留,但内存可能未被映射为这个过程的权利?不确定,只是问。 – poukill