boost::circular buffer等效于文件

boost::circular_buffer equivalent for files?

本文关键字:于文件 文件 circular buffer boost      更新时间:2023-10-16

我正在寻找一个可以在磁盘上获得循环缓冲区的库
Boost中也有类似的东西,但它是一个基于内存的容器:circular _buffer。

您可以将其称为任何您认为自然的名称。

您正在查找内存映射文件。

使用正确的分配器,可以使容器在这个内存映射区域中进行分配。这将使容器"在磁盘上"。

我会看看Boost Circularbuffer是否直接支持这一点

更新是。

最棒的是,这为您提供了充分的可能性,甚至可以使用IPC同步和线程同步。使用"专用"内存映射,您可以将缓冲区映射为可读写,而无需在某些进程中将更改写回磁盘。

概念证明:

在Coliru上直播 cco

#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];
};
int main()
{
    bip::managed_mapped_file mmf(bip::open_or_create, "/tmp/circ_buffer.bin", 4ul << 20);
    typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;
    boost::circular_buffer<message, allocator> instance(100, mmf.get_segment_manager());
}

在Coliru上,文件大小受到限制,这是可以理解的。