boost :: circular_buffer等效于文件?

时间:2015-03-25 14:33:02

标签: c++ boost disk circular-buffer

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

1 个答案:

答案 0 :(得分:6)

你可以随意调用它。

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

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

我会看看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());
}

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