Boost::Interprocess 与复杂的嵌套类

Posted

技术标签:

【中文标题】Boost::Interprocess 与复杂的嵌套类【英文标题】:Boost::Interprocess with complex, nested classes 【发布时间】:2010-08-11 15:16:04 【问题描述】:

我想我终于掌握了 boost:interprocess 库的基础知识,并且在处理包含一些成员变量的相对简单的类时,我已经成功地使用了它,这些成员变量都是标准数据类型。

但是,我现在面临将一个相当复杂的类推到进程间共享内存中的问题,而且我一点运气都没有。我希望这里有人可以提供帮助(或者可以引导我寻求替代解决方案)。将其剥离为我希望的基础知识,我有以下几点:

// these first two classes are from the Gnu Scientific Library, so I can't  
// (or won't) be able to mess with the definition of these structures.  
typedef struct   
  
    size_t size1;  
    size_t size2;  
    size_t tda;  
    double * data;  
    gsl_block * block;  
    int owner;  
 gsl_matrix;  

typedef struct   

    size_t size1;
    size_t size2;
    size_t tda;
    int * data;
    gsl_block_int * block;
    int owner;
 gsl_matrix_int;

class MyNumbersClass

    gsl_matrix_int* m_pIntMatrix;
    gsl_matrix*     m_pDblMatrix;
    unsigned int iVal1;
    unsigned int iVal2;


class MyOtherDataClass

    std::vector<int> m_vInputs;
    std::vector<double> m_vOutputs;
    std::string m_sTitle;
    bool m_bCorrect;


class SharedClass

    MyNumbersClass* m_pFirstNumbers;
    std::vector<double> m_vDblData;
    std::vector<MyOtherDataClass> m_vOtherData;

我需要做的是在进程间内存空间中创建一个 SharedClass 对象的向量,以便多个进程可以访问数据。无论我尝试什么,我似乎都找不到解决方案。似乎应该有一种更简单的方法,而不是让每个成员变量沿途成为进程间版本(interprocess::vector、interprocess::string、offset_ptr 等),但也许不是。即便如此,我也不确定如何处理所有分配器等,更不用说 GSL 库结构了。

救命!

【问题讨论】:

【参考方案1】:

您的问题在于 STL 容器。如您所料,您需要使用 boost::interprocess 容器。

有关使用字符串的示例,请参阅my answer here。 向量见this。

干杯

【讨论】:

以上是关于Boost::Interprocess 与复杂的嵌套类的主要内容,如果未能解决你的问题,请参考以下文章

boost::interprocess::file_lock 与 std::ostream 一起使用时的错误行为

使用 Boost Interprocess SharedMemory 构建错误

Boost.Interprocess内存位置

boost::interprocess::string 转换为 char*

boost::interprocess::interprocess_condition::wait 在等待时不会原子地解锁互斥锁

boost::interprocess::message_queue 权限被拒绝