boost::circular_buffer equivalent for files?

馋奶兔 提交于 2019-11-30 16:32:18

You can call it whatever you think is natural.

You're looking for memory mapped files.

Using the right allocator, you can make containers be allocating in this memory mapped region. That would make the container "on disk".

I'll see whether Boost Circularbuffer supports this directly.

Update Yes.

The best thing is, this gives you full possibility to even use IPC synchronization and thread synchronization. Using a "private" memory map you could map the buffer read-writable without writing changes back to disk in some of the processes.

Proof of concept:

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

¹ On Coliru the filesize is - understandably constrained.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!