boost :: circular_buffer等效于文件?

Pie*_*tro 1 c++ boost disk circular-buffer

我正在寻找一个允许在磁盘上获得循环缓冲区的库.
在Boost中有类似的东西,但它是一个基于内存的容器:circular_buffer.

seh*_*ehe 7

你可以把它叫做任何你认为很自然的东西.

您正在寻找内存映射文件.

使用正确的分配器,您可以在此内存映射区域中分配容器.这将使容器"在磁盘上".

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

更新是.

最棒的是,这使您甚至可以使用IPC同步和线程同步.使用"私有"内存映射,您可以映射缓冲区可读写,而无需在某些进程中将更改写回磁盘.

概念证明:

Live On Coliru ¹

#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());
}
Run Code Online (Sandbox Code Playgroud)

¹在Coliru上,文件大小是可以理解的.