在磁盘上使用循环缓冲区 [英] Using a circular buffer on disk
问题描述
我试图使用Boost在磁盘上创建内存映射的循环缓冲区,我读了以下答案: https://stackoverflow .com/a/29265629/8474732
I was trying to create a memory-mapped circular buffer on disk using Boost, and I read this answer: https://stackoverflow.com/a/29265629/8474732
但是,我很难读取所写的循环缓冲区.我尝试在实例"变量上执行push_back,现在实例的大小为1.太好了.但是我将如何读回内容?还是稍后再push_back其他元素?使用相同的分配器和mmf创建另一个实例将显示该实例的大小为0.我想要一个可以打开磁盘上的文件并在循环缓冲区中push_back一个值,然后返回的函数.我想多次调用此函数.我正在尝试做的一个例子(来自链接的答案):
However, I have a hard time reading the circular buffer that was written. I tried to do a push_back on the "instance" variable, now the instance has size 1. Great. But how would I read the contents back? Or push_back additional elements at a later time? Creating another instance from the same allocator and mmf shows that the instance has size 0. I would want a function that can open a file on disk and push_back a value in the circular buffer, then return. I would want to call this function multiple times. An example of what I'm trying to do (derived from the linked answer):
#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];
};
void writeFunction() {
bip::managed_mapped_file mmf(bip::open_or_create, "./circ_buffer.bin", 4ul << 10);
typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;
boost::circular_buffer<message, allocator> instance(10, mmf.get_segment_manager());
struct message test;
instance.push_back( test );
}
当我想写入磁盘上的循环缓冲区时,我想调用此函数,并且还可以用另一个函数(类似这样)来读取它:
I would like to call this function when I want to write to the circular buffer on disk, and also be able to read it with another function (something like this):
void readFunction() {
bip::managed_mapped_file mmf(bip::open_or_create, "./circ_buffer.bin", 4ul << 10);
typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;
boost::circular_buffer<message, allocator> instance(10, mmf.get_segment_manager());
for(struct message msg : instance) {
cout << msg.string;
}
}
感谢您的帮助!
推荐答案
链接的帖子是一个最小的示例,仅显示circular_buffer
支持Boost进程间内存段所需的有状态分配器.
The linked post was a mimimal example that ONLY showed that the stateful allocator required for Boost Interprocess memory segments are supported in circular_buffer
.
要从该段中检索循环缓冲区本身,您需要在共享内存段中构造对象本身(除了传递shared-mem-allocator之外).
To retrieve the circular buffer itself from the segment you need to construct the object itself in the shared memory segment (in addition to passing the shared-mem-allocator).
没有关注效率,这只是一个愚蠢的演示:
No attention was payed to efficiency, this is just a dumb demo:
#include <boost/circular_buffer.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <iostream>
namespace bip = boost::interprocess;
struct message {
int data[32];
};
void writeFunction() {
bip::managed_mapped_file mmf(bip::open_or_create, "./circ_buffer.bin", 4ul << 10);
typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;
typedef boost::circular_buffer<message, allocator> circ_buf;
auto& instance = *mmf.find_or_construct<circ_buf>("named_buffer")(10, mmf.get_segment_manager());
struct message test;
instance.push_back( test );
std::cout << "pushed a message (" << instance.size() << ")\n";
}
void readFunction() {
bip::managed_mapped_file mmf(bip::open_or_create, "./circ_buffer.bin", 4ul << 10);
typedef bip::allocator<message, bip::managed_mapped_file::segment_manager> allocator;
typedef boost::circular_buffer<message, allocator> circ_buf;
auto& instance = *mmf.find_or_construct<circ_buf>("named_buffer")(10, mmf.get_segment_manager());
struct message test;
while (!instance.empty()) {
test = instance.front();
instance.pop_front();
std::cout << "popped a message (" << instance.size() << ")\n";
}
}
int main() {
writeFunction();
writeFunction();
writeFunction();
readFunction();
}
打印
{"a":["1","2","3","4","5","6"]}
4
4
No such node (b)
element_at_checked
这篇关于在磁盘上使用循环缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!