c++ - 在磁盘上使用循环缓冲区

标签 c++ boost memory-mapped-files boost-interprocess boost-circularbuffer

我试图使用 Boost 在磁盘上创建一个内存映射循环缓冲区,我读到了这个答案:https://stackoverflow.com/a/29265629/8474732

但是,我很难读取写入的循环缓冲区。我尝试对“实例”变量执行 push_back,现在实例的大小为 1。太棒了。但是我该如何读回内容呢?还是稍后 push_back 其他元素?从同一个分配器和 mmf 创建另一个实例显示该实例的大小为 0。我想要一个可以打开磁盘上的文件并 push_back 循环缓冲区中的值的函数,然后返回。我想多次调用这个函数。我正在尝试做的一个例子(来自链接的答案):

#include <boost/circular_buffer.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>

namespace bip = boost::interprocess;

struct message {
    int data[32];
};

void writeFunction() {
    bip::managed_mapped_file mmf(bip::open_or_create, "./circ_buffer.bin", 4ul << 10);
    typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;

    boost::circular_buffer<message, allocator> instance(10, mmf.get_segment_manager());

    struct message test;
    instance.push_back( test );
}

当我想写入磁盘上的循环缓冲区时,我想调用这个函数,并且还能够用另一个函数(像这样)读取它:

void readFunction() {
    bip::managed_mapped_file mmf(bip::open_or_create, "./circ_buffer.bin", 4ul << 10);
    typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;

    boost::circular_buffer<message, allocator> instance(10, mmf.get_segment_manager());

    for(struct message msg : instance) {
        cout << msg.string;
    }
}

感谢您的帮助!

最佳答案

链接的帖子是一个简单的示例,它仅表明 circular_buffer 支持 Boost Interprocess 内存段所需的有状态分配器。

要从段中检索循环缓冲区本身,您需要在共享内存段中构造对象本身(除了传递共享内存分配器之外)。

演示

没有注意效率,这只是一个愚蠢的演示:

Live On Coliru

#include <boost/circular_buffer.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <iostream>

namespace bip = boost::interprocess;

struct message {
    int data[32];
};

void writeFunction() {
    bip::managed_mapped_file mmf(bip::open_or_create, "./circ_buffer.bin", 4ul << 10);
    typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;
    typedef boost::circular_buffer<message, allocator> circ_buf;

    auto& instance = *mmf.find_or_construct<circ_buf>("named_buffer")(10, mmf.get_segment_manager());

    struct message test;
    instance.push_back( test );
    std::cout << "pushed a message (" << instance.size() << ")\n";
}

void readFunction() {
    bip::managed_mapped_file mmf(bip::open_or_create, "./circ_buffer.bin", 4ul << 10);
    typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;
    typedef boost::circular_buffer<message, allocator> circ_buf;

    auto& instance = *mmf.find_or_construct<circ_buf>("named_buffer")(10, mmf.get_segment_manager());

    struct message test;
    while (!instance.empty()) {
        test = instance.front();
        instance.pop_front();
        std::cout << "popped a message (" << instance.size() << ")\n";
    }
}

int main() {
    writeFunction();
    writeFunction();
    writeFunction();

    readFunction();
}

打印

{"a":["1","2","3","4","5","6"]}
4
4
No such node (b)
element_at_checked

关于c++ - 在磁盘上使用循环缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45722805/

相关文章:

c++ - 在 SDL 中渲染静止图像

C++ boost/multiprecision : fatal error: mpfr. h: 没有那个文件或目录

c++ - 内存分配器

c++ - 使用 Boost::mapped_file 时,为什么文件的大小似乎上限为 65,356 字节?

c++ - 结构 vector 上的 MemCpy

c++ - 如何在引用上设置数据断点

c++ - 如何更改 C++ 中的 vector 项?

c++ - 在传递给函数的 char 指针上使用下标运算符不会修改值。为什么?

c++ - 未能尝试定义 Boost::mp11::find_if 谓词

c++ - 由 traits [policies, actually] 实例化的类成员函数