boost :: circular_buffer等效文件? [英] boost::circular_buffer equivalent for files?

查看:182
本文介绍了boost :: circular_buffer等效文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个允许在磁盘上获得循环缓冲区的库。

在Boost中有一些类似的东西,但它是一个基于内存的容器: circular_buffer

解决



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



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



我会看看Boost Circularbuffer是否直接支持这个。



更新是的。



最好的事情是,这样就可以使用IPC同步和线程同步。使用私有内存映射,您可以将缓冲区映射为可读写,无需在某些进程中将更改写回磁盘。



概念证明:



Live On Coliru ¹

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

命名空间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>分配器

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






¹在Coliru是可以理解的约束。


I am looking for a library which allows to get a circular buffer on disk.
In Boost there is something similar, but it is an in memory based container: circular_buffer.

解决方案

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.

这篇关于boost :: circular_buffer等效文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆