boost::不在共享内存中的容器的进程间容器

boost::不在共享内存中的容器的进程间容器,boost,containers,interprocess,allocator,Boost,Containers,Interprocess,Allocator,我有一个类型为的示例演示程序。 但是我喜欢在进程内存中使用普通类。 有人能帮我写一个不带参数的构造函数,让类在我当前的进程内存中初始化吗 #include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/managed_shared_memory.hpp>

我有一个类型为的示例演示程序。 但是我喜欢在进程内存中使用普通类。 有人能帮我写一个不带参数的构造函数,让类在我当前的进程内存中初始化吗

#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <shmfw/serialization/interprocess_vector.hpp>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */


using namespace boost::interprocess;
//Alias an STL-like allocator of ints that allocates ints from the segment
typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;

//Alias a vector that uses the previous STL-like allocator
typedef vector<int, ShmemAllocator> MyVector;

typedef allocator<void, managed_shared_memory::segment_manager >                           void_allocator;

class MyStruct {
public:
    MyVector myVector;
    //Since void_allocator is convertible to any other allocator<T>, we can simplify
    //the initialization taking just one allocator for all inner containers.
    MyStruct ( const void_allocator &void_alloc )
        : myVector ( void_alloc )
    {}
    // Thats what I like to have       
    //MyStruct ()
    //    : myVector ( ?? )
    //{}
};

int main () {

    // I would like to have something like that working and also the shm stuff below
    // MyStruct x;


    managed_shared_memory segment;
    //A managed shared memory where we can construct objects
    //associated with a c-string
    try {
         segment =  managed_shared_memory( create_only, "MySharedMemory", 65536 );
    } catch (...){
        segment = managed_shared_memory( open_only, "MySharedMemory" );
    }

    //Initialize the STL-like allocator
    const ShmemAllocator alloc_inst ( segment.get_segment_manager() );

    MyStruct *myStruct_src =  segment.find_or_construct<MyStruct> ( "MyStruct" ) ( alloc_inst );
    srand (time(NULL));
    myStruct_src->myVector.push_back ( rand() );

    MyStruct *myStruct_des =  segment.find_or_construct<MyStruct> ( "MyStruct" ) ( alloc_inst );

    for ( size_t i = 0; i < myStruct_src->myVector.size(); i++ ) {
        std::cout << i << ": " << myStruct_src->myVector[i] << " = " << myStruct_des->myVector[i] << std::endl;
    if(myStruct_src->myVector[i] != myStruct_des->myVector[i]) {
      std::cout << "Something went wrong!" << std::endl;
    }
    }


    //segment.destroy<MyVector> ( "MyVector" );
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括/*srand,兰特*/
#包括/*时间*/
使用名称空间boost::interprocess;
//别名类似STL的整数分配器,用于从段中分配整数
typedef分配器ShmemAllocator;
//别名使用以前类似STL的分配器的向量
typedef向量MyVector;
typedef分配器void_分配器;
类MyStruct{
公众:
MyVector MyVector;
//由于void_分配器可转换为任何其他分配器,因此我们可以简化
//初始化只对所有内部容器使用一个分配器。
MyStruct(const void\u分配器和void\u alloc)
:myVector(void\u alloc)
{}
//这就是我想要的
//MyStruct()
//:myVector(??)
//{}
};
int main(){
//我希望有这样的工作,也shm的东西下面
//MyStruct x;
托管共享内存段;
//我们可以在其中构造对象的托管共享内存
//与c字符串关联
试一试{
段=托管共享内存(仅创建“MySharedMemory”,65536);
}捕获(…){
段=托管共享内存(仅打开“MySharedMemory”);
}
//初始化类似STL的分配器
const ShmemAllocator alloc_inst(段.get_段管理器());
MyStruct*MyStruct_src=segment.find_或_构造(“MyStruct”)(alloc_inst);
srand(时间(空));
myStruct_src->myVector.push_back(rand());
MyStruct*MyStruct_des=segment.find_或_构造(“MyStruct”)(alloc_inst);
对于(size\u t i=0;imyVector.size();i++){

std::cout如果更改分配器类型,则更改容器(这就是编译时模板实例化的性质)

从技术上讲,您可以设计一个类型擦除的分配器(a la
std::function
boost::any_iterator
),但这可能会导致糟糕的性能。此外,它仍然需要所有分配器在所有静态已知属性中进行对应,从而降低灵活性

实际上,我建议将
MyStruct
模板化在
Allocator
类型上,以用于任何嵌入式容器。然后在构造函数中具体使用这样一个分配器:

// Variant to use on the heap:
using HeapStruct  = MyStruct<std::allocator>;
// Variant to use in shared memory:
using ShmemStruct = MyStruct<BoundShmemAllocator>;
//堆上要使用的变量:
使用HeapStruct=MyStruct;
//要在共享内存中使用的变量:
使用ShmemStruct=MyStruct;
演示程序:

#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/range/algorithm.hpp>
#include <iostream>
#include <cassert>

namespace bip = boost::interprocess;
template <typename T> 
    using BoundShmemAllocator = bip::allocator<T, bip::managed_shared_memory::segment_manager>;

///////////////////////////////////////////////////////////////
// Your MyStruct, templatized for an Allocator class template

template <template<typename...> class Allocator>
class MyStruct {
public:
    bip::vector<int,    Allocator<int>    > ints;
    bip::vector<double, Allocator<double> > doubles;

    MyStruct(const Allocator<void>& void_alloc = {})
        : ints(void_alloc),
          doubles(void_alloc)
    {}
};

// Variant to use on the heap:
using HeapStruct  = MyStruct<std::allocator>;
// Variant to use in shared memory:
using ShmemStruct = MyStruct<BoundShmemAllocator>;

//
///////////////////////////////////////////////////////////////

int main() {
    srand(time(NULL));

    // You can have something like this working: 
    HeapStruct x; // and also the shm stuff below
    std::generate_n(std::back_inserter(x.ints),    20, &std::rand);
    std::generate_n(std::back_inserter(x.doubles), 20, &std::rand);

    // A managed shared memory where we can construct objects
    bip::managed_shared_memory segment = bip::managed_shared_memory(bip::open_or_create, "MySharedMemory", 65536);
    BoundShmemAllocator<int> const shmem_alloc(segment.get_segment_manager());

    auto src = segment.find_or_construct<ShmemStruct>("MyStruct")(shmem_alloc);
    src->ints.insert(src->ints.end(),       x.ints.begin(),    x.ints.end());
    src->doubles.insert(src->doubles.end(), x.doubles.begin(), x.doubles.end());

    auto des = segment.find_or_construct<ShmemStruct>("MyStruct")(shmem_alloc);

    std::cout << "-------------------------";
    boost::copy(src->ints, std::ostream_iterator<int>(std::cout << "\nsrc ints: ", "; "));
    boost::copy(des->ints, std::ostream_iterator<int>(std::cout << "\ndes ints: ", "; "));
    std::cout << "\n-------------------------";
    boost::copy(src->doubles, std::ostream_iterator<double>(std::cout << "\nsrc doubles: ", "; "));
    boost::copy(des->doubles, std::ostream_iterator<double>(std::cout << "\ndes doubles: ", "; "));

    assert(src->ints.size()    == des->ints.size());
    assert(src->doubles.size() == des->doubles.size());
    assert(boost::mismatch(src->ints,    des->ints)    == std::make_pair(src->ints.end(),    des->ints.end()));
    assert(boost::mismatch(src->doubles, des->doubles) == std::make_pair(src->doubles.end(), des->doubles.end()));

    segment.destroy<ShmemStruct>("MyStruct");
}
#包括
#包括
#包括
#包括
#包括
#包括
名称空间bip=boost::进程间;
模板
使用BoundShmemAllocator=bip::allocator;
///////////////////////////////////////////////////////////////
//您的MyStruct,已模板化为分配器类模板
模板
类MyStruct{
公众:
向量整数;
bip::向量加倍;
MyStruct(常量分配器&void_alloc={})
:ints(void_alloc),
双打(无效)
{}
};
//要在堆上使用的变量:
使用HeapStruct=MyStruct;
//要在共享内存中使用的变量:
使用ShmemStruct=MyStruct;
//
///////////////////////////////////////////////////////////////
int main(){
srand(时间(空));
//您可以让这样的东西工作:
HeapStruct x;//还有下面的shm内容
std::generate_n(std::back_inserter(x.ints)、20和std::rand);
std::generate_n(std::back_inserter(x.doubles)、20和std::rand);
//我们可以在其中构造对象的托管共享内存
bip::managed_shared_memory段=bip::managed_shared_memory(bip::open_或_create,“MySharedMemory”,65536);
BoundShmemAllocator const shmem_alloc(segment.get_segment_manager());
auto src=segment.find_或_构造(“MyStruct”)(shmem_alloc);
src->ints.insert(src->ints.end(),x.ints.begin(),x.ints.end());
src->doubles.insert(src->doubles.end(),x.doubles.begin(),x.doubles.end());
自动des=段。查找或构造(“MyStruct”)(shmem\u alloc);
std::cout int,std::ostream_迭代器(std::cout int,std::ostream_迭代器(std::cout int.size());
断言(src->doubles.size()==des->doubles.size());
断言(boost::mismatch(src->ints,des->ints)==std::make_pair(src->ints.end(),des->ints.end());
断言(boost::mismatch(src->doubles,des->doubles)==std::make_pair(src->doubles.end(),des->doubles.end());
段。销毁(“MyStruct”);
}

有人知道如何将这个答案与作用域分配程序结合起来吗?-->《谢谢》中更详细地介绍了这个问题