如何通过boost :: interprocess在向量中构造向量

时间:2014-01-08 05:55:46

标签: boost interprocess boost-interprocess inter-process-communicat

我已经提到了关于“在shared_memory中创建向量”的提升示例。 现在我的数据结构如下:

数据结构:

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

1 个答案:

答案 0 :(得分:1)

除了“明显的”拼写错误之外,您还尝试将IPC矢量(MyVector*)分配给GlobalFuncall构造函数中的标准向量。那永远不会奏效。 C ++是一种强类型语言,因此如果要分配 [1] ,则必须匹配类型。

除此之外,似乎存在一个概念问题:

  • 如果目标是使数据收集大于适合物理内存,那么共享内存本身就无济于事。你想看一下内存映射文件
  • 如果您想要共享内存,因为您可以在进程之间共享它(因此 Boost Interprocess ),您将 需要 来考虑进程同步,或者你会因为数据竞争而看到复杂的错误。
  • 无法 安全地将原始指针存储在此容器中。相反,将实际元素存储在那里(或者可能查看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不支持所需的IPC机制:/