在磁盘上使用循环缓冲区
Using a circular buffer on disk
我试图使用 Boost 在磁盘上创建一个内存映射的循环缓冲区,我读到了这个答案:https://stackoverflow.com/a/29265629/8474732
但是,我很难读取写入的循环缓冲区。我试图对"实例"变量进行push_back,现在实例的大小为 1。伟大。但是我该如何读回内容呢?还是稍后push_back其他元素?从相同的分配器和 mmf 创建另一个实例会显示该实例的大小为 0。我想要一个可以在磁盘上打开文件并在循环缓冲区中push_back值的函数,然后返回。我想多次调用此函数。我正在尝试做的事情的示例(来自链接的答案(:
#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 );
}
当我想写入磁盘上的循环缓冲区时,我想调用这个函数,并且还可以使用另一个函数读取它(如下所示(:
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;
}
}
感谢您的任何帮助!
链接的帖子是一个模拟示例,仅显示 Boost 进程间内存段所需的有状态分配器在circular_buffer
中受支持。
要从段中检索循环缓冲区本身,您需要在共享内存段中构造对象本身(除了传递共享内存分配器(。
演示
没有关注效率,这只是一个愚蠢的演示:
住在科里鲁
#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
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- 有没有办法重复循环循环?
- Qt有循环缓冲吗?
- 虽然第三次循环循环,尽管在我眼中没有满足它的条件,因此打印了一个空字符串
- 避免以不同的迭代剂的循环循环避免几乎相同的代码重复
- 循环循环保存到数组,然后访问;
- 未定义的行为怪癖:在缓冲区外读取导致循环永远不会终止
- 对于循环循环仅 3 次
- 处理媒体循环循环循环通过向量中的对象的各个方面
- 将C转换为循环循环循环
- 关于如何正确循环循环并找到最小的价值,需要基本C 的帮助
- 我如何循环循环switch语句检查重新检查char?(C )
- 在嵌套循环/循环不变量中检查一次
- 只有在充满for循环的情况下才能进行开放缓冲播放
- 调用linearize后奇怪的boost循环缓冲行为