【问题标题】:Copy std::vector to boost::interprocess::vector复制 std::vector 到 boost::interprocess::vector
【发布时间】:2018-07-24 17:08:23
【问题描述】:

我想使用 boost 进程间共享对象向量。对象来自以下结构:

struct Foo
    {
        int id;
        float time;
        bool isFoo;

        float myArray[7];

        std::vector<int> vectorData;
    };

我正在创建 boost 进程间分配器和进程间向量:

typedef allocator<Foo, managed_shared_memory::segment_manager>  FooAllocator;

typedef std::vector<Foo, FooAllocator> FooVector;

在我的主函数中,我初始化内存段、分配器和向量,基于:

boost -> creating vectors in shared memory

所以:

managed_shared_memory mem_segment(open_or_create, "MemShare", 65536);
const FooAllocator alloc_inst(mem_segment.get_segment_manager());

fooVector = mem_segment.find_or_construct<FooVector>("FooVector")(alloc_inst);

现在,这适用于 Foo 结构中除向量之外的所有数据类型。因此,如果我尝试分享这个,我会从 Foo 中获取所有成员,而对于矢量数据,我会得到“未定义的内存位置” 我知道 std::vector 不能直接共享。所以我用 boost::interprocess:vector 创建了新的 Foo 结构

struct FooInter
    {
        int id;
        float time;
        bool isFoo;

        float myArray[7];
        MyVector* pointcloud;
    };

MyVector 在哪里:

typedef allocator<int, managed_shared_memory::segment_manager> VectorAllocator;
typedef boost::interprocess::vector<int, VectorAllocator> MyVector;

我正在为 MyVector 分配内存,

const VectorAllocator vec_alloc_inst(mem_segment.get_segment_manager());
MyVector* tmpVec = mem_segment.construct<MyVector>("MyVector")(vec_alloc_inst);

然后我现在尝试做的是将 Foo 映射到 FooInter。我在 for 循环中映射矢量数据:

    for (int t = 0; t < foo.vectorData.size()-1; t++) {
        tmpVec->push_back(foo.vectorData[t]);
    }

然后将tmpVec复制到fooInter.vectorData中:

memcpy(fooInter.pointcloud, tmpVec, sizeof(int) * tmpVec->size());

这有效,但不适用于 foo.vectorData 的整个大小。所以它适用于 100 个项目,但如果我使用 foo.vectorData.size() 它会返回错误的内存分配。

有人可以帮我解决这个问题吗?我需要知道共享这种类型结构的正确方法。我觉得我所做的是完全错误的。也许我需要将向量序列化为字符串或类似的东西。

编辑:

根据sehe的回答:

我有来自类型的对象消息:

struct Foo
    {
        int id;
        float time;
        bool isFoo;

        float myArray[7];

        std::vector<int> pointcloud;
    };

我需要在 inter_foos 中传递该对象。所以在sehe的代码中:

int main() {
    auto segment = Shared::open();
    auto& inter_foos = *segment.find_or_construct<InterFoos>("InterFoos")(segment.get_segment_manager());

    // you can directly append to the shared memory vector
    int nextid = inter_foos.size();
    //instead of this
    inter_foos.push_back({++nextid, 0, true, {.1,.2,.3,.4,.5,.6,.7}, Ints ({10,20,30}, segment.get_segment_manager()) });
    //i need this
        inter_foos.push_back({msg.id, msg.time, true, msg.myArray, Ints (msg.pointcloud, segment.get_segment_manager()) });

    //i can't pass msg.poincloud to this object!!!

    // or copy from a non-shared vector:
    std::vector<Foo> const local {
        {++nextid, 0, true, {.1,.2,.3,.4,.5,.6,.7}, {10,20,30} },
        {++nextid, 1, true, {.2,.3,.4,.5,.6,.7,.8}, {20,30,40} },
        {++nextid, 2, true, {.3,.4,.5,.6,.7,.8,.9}, {30,40,50} },
    };

    for (auto& local_foo : local)
        inter_foos.emplace_back(local_foo);

    // print the current contents
    for (auto& foo : inter_foos)
        std::cout << foo << "\n"; 
}

【问题讨论】:

    标签: c++ vector boost dynamic-memory-allocation interprocess


    【解决方案1】:

    您可以使用

    中的方法

    查看此演示¹

    Live On Coliru

    #include <boost/interprocess/managed_shared_memory.hpp>
    
    #include <boost/interprocess/allocators/allocator.hpp>
    #include <boost/interprocess/containers/vector.hpp>
    #include <boost/container/scoped_allocator.hpp>
    
    #include <iostream>
    #include <vector>
    
    namespace Shared {
        namespace bip = boost::interprocess;
        namespace bc  = boost::container;
    
        using Segment                     = bip::managed_shared_memory;
        using Manager                     = Segment::segment_manager;
        template <typename T> using Alloc = bc::scoped_allocator_adaptor<bip::allocator<T, Manager> >;
        template <typename T> using Vector= bip::vector<T, Alloc<T> >;
    
        Segment open() { return { bip::open_or_create, "MySharedMemory", 10ul<<20 }; }
    
        template <typename Alloc = Alloc<void> >
        struct Foo {
            using allocator_type = typename Alloc::template rebind<Foo>::other;
            using Ints = bip::vector<int, typename Alloc::template rebind<int>::other>;
    
            Foo(int id, float time, bool isFoo = false, std::initializer_list<float> floats = {}, Ints data = {})
                : id(id), time(time), isFoo(isFoo), vectorData(std::move(data))
            {
                std::copy_n(floats.begin(), std::min(floats.size(), 7ul), myArray);
            }
    
            template <typename OA, typename A>
            Foo(Foo<OA> const& other, A alloc = {}) 
                : id(other.id), time(other.time), isFoo(other.isFoo),
                  vectorData(other.vectorData.begin(), other.vectorData.end(), alloc)
            {
                std::copy(std::begin(other.myArray), std::end(other.myArray), myArray);
            }
    
            int   id;
            float time;
            bool  isFoo;
            float myArray[7] = {};
            Ints  vectorData;
        };
    
        template <typename A>
        std::ostream& operator<<(std::ostream& os, Foo<A> const& f) {
            os << "{" 
               << f.id << ", "
               << std::fixed << f.time << ", "
               << std::boolalpha << f.isFoo << ", "
               << "[";
    
            std::copy(std::begin(f.myArray), std::end(f.myArray), std::ostream_iterator<float>(std::cout, ";"));
            os << "], [";
            std::copy(std::begin(f.vectorData), std::end(f.vectorData), std::ostream_iterator<int>(std::cout, ";"));
            return os << "] }";
        }
    
    }
    
    using Foo       = Shared::Foo<std::allocator<void> >;
    using InterFoo  = Shared::Foo<>;
    
    using InterFoos = Shared::Vector<InterFoo>;
    using Ints      = Shared::Vector<int>;
    
    int main() {
        auto segment = Shared::open();
        auto& inter_foos = *segment.find_or_construct<InterFoos>("InterFoos")(segment.get_segment_manager());
    
        // you can directly append to the shared memory vector
        int nextid = inter_foos.size();
        inter_foos.push_back({++nextid, 0, true, {.1,.2,.3,.4,.5,.6,.7}, Ints ({10,20,30}, segment.get_segment_manager()) });
    
        // or copy from a non-shared vector:
        std::vector<Foo> const local {
            {++nextid, 0, true, {.1,.2,.3,.4,.5,.6,.7}, {10,20,30} },
            {++nextid, 1, true, {.2,.3,.4,.5,.6,.7,.8}, {20,30,40} },
            {++nextid, 2, true, {.3,.4,.5,.6,.7,.8,.9}, {30,40,50} },
        };
    
        for (auto& local_foo : local)
            inter_foos.emplace_back(local_foo);
    
        // print the current contents
        for (auto& foo : inter_foos)
            std::cout << foo << "\n"; 
    }
    

    打印:

    {1, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
    {2, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
    {3, 1.000000, true, [0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;], [20;30;40;] }
    {4, 2.000000, true, [0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;0.900000;], [30;40;50;] }
    

    第二次运行:

    {1, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
    {2, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
    {3, 1.000000, true, [0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;], [20;30;40;] }
    {4, 2.000000, true, [0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;0.900000;], [30;40;50;] }
    {5, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
    {6, 0.000000, true, [0.100000;0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;], [10;20;30;] }
    {7, 1.000000, true, [0.200000;0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;], [20;30;40;] }
    {8, 2.000000, true, [0.300000;0.400000;0.500000;0.600000;0.700000;0.800000;0.900000;], [30;40;50;] }
    

    作用域分配器

    请注意:

        for (auto& local_foo : local)
            inter_foos.emplace_back(local_foo);
    

    之所以起作用,是因为 Boost 的容器实现支持并传递了作用域分配器。如果你不使用它,事情会是这样的:Live On Coliru

    template <typename T> using Alloc = bip::allocator<T, Manager>;
    // ...
    
    for (auto& local_foo : local)
        inter_foos.emplace_back(local_foo, segment.get_segment_manager());
    

    ¹ 使用映射文件,因为 COLIRU 不支持共享内存

    【讨论】:

    • 感谢您的回复。我不知道如何使用 fooInter.pointcloud 中的值初始化 inter_foos。您基本上是在使用 initilaizer_list。那么如何使用向量而不是常量列表呢?
    • 你能告诉我你现在卡在什么地方吗?在我的方法中,pointcloud 不再是原始指针成员(这会造成您的问题,因为那时您必须手动进行内存管理)。 [提示:您可以编辑我的实时示例,直到它反映您的问题]
    • 代替这个:inter_foos.push_back({++nextid, 0, true, {.1,.2,.3,.4,.5,.6,.7}, Ints ( {10,20,30},segment.get_segment_manager()) });我需要这样做: inter_foos.push_back({msg.id, msg.time, msg.isKeyframe, msg.myArray, Ints (msg.pointcloud, segment.get_segment_manager()) });其中 msg 是来自的对象:struct Foo { int id;浮动时间;布尔 isFoo;浮动我的数组[7]; std::vector 点云; };你能明白我的意思吗?我如何将点云放入 inter_foos。我的 myArray 也有问题
    • 呃,评论线程不会以这种方式正常工作。在 Coliru(或 pastebin 或其他)上发布示例,编辑您的原始问题或考虑发布新问题。
    • 我编辑了我的问题,请检查代码中的 cmets
    【解决方案2】:

    我无法想象使用memcpy 复制MyVector 会很好地工作。当然你需要的是:

    void FooInter::setVector(const std::vector<int>& vec) {
        const VectorAllocator vec_alloc_inst(mem_segment.get_segment_manager());
        const auto tmp = mem_segment.construct<MyVector>("MyVector")(vec_alloc_inst);
        tmp->insert(tmp->begin(), vec.begin(), vec.end());
        pointcloud = tmp;
    }
    

    也就是说,构造pointcloud指向的对象,然后插入进去。

    【讨论】:

    • 我在插入时遇到读取访问冲突,可能是因为我们尝试执行此操作时未分配点云
    • 如果你的意思是“每个人都应该知道这是非法的并导致Undefined Behaviour”,请不要说“我无法想象”
    • @DuskoMurtovski 对不起。这是我的一个想法。固定使用 tmp。
    • @MartinBonner 谢谢,我在 next_capacity.hpp 中得到 std:lenght_error。这实际上与我尝试使用 for 循环复制矢量数据时得到的错误相同。 vec的长度是3646400,会不会是这个问题?
    • @sehe :“每个人都应该知道这是非法的” - a) 只有当他们知道它不是 POD 时; b) 我们是否真的需要把所有的东西都写得完全不靠谱?
    猜你喜欢
    • 2011-09-17
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多