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