[英]c++ boost write memory mapped file
I am searching for fast writing a file using C++ and boost library. 我正在寻找使用C ++和boost库快速编写文件。 And I would like to use memory mapped file. 我想使用内存映射文件。 But Almost all example is about reading. 但几乎所有的例子都是关于阅读。
Working is very simple. 工作很简单。 There is a string array. 有一个字符串数组。 The array elements is about 2 millions. 数组元素大约有2百万。
ofstream outFile("text.txt");
for (int i = 0; i < 2000000; ++i) {
outFile << strArray[i] << "\n";
}
outFile.close();
How can I do it using memory mapped file? 我怎么能用内存映射文件呢? Where can I find writing file using memory mapped file? 我在哪里可以找到使用内存映射文件的文件?
Thank you for your concerning. 谢谢你的关心。
You could use Boost Iostreams mapped_file{_sink,_source}
for this. 您可以使用Boost Iostreams mapped_file{_sink,_source}
。
Although Boost Interprocess does use mapped files, but you'd be better off using IOstreams for this kind of raw access. 尽管Boost Interprocess确实使用了映射文件,但您最好使用IOstream进行这种原始访问。
See http://www.boost.org/doc/libs/1_50_0/libs/iostreams/doc/classes/mapped_file.html 请参阅http://www.boost.org/doc/libs/1_50_0/libs/iostreams/doc/classes/mapped_file.html
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/iostreams/stream.hpp>
#include <vector>
namespace bio = boost::iostreams;
int main() {
using namespace std;
vector<string> strArray(2000000);
bio::mapped_file_params params;
params.path = "text.txt";
params.new_file_size = 30ul << 30;
params.flags = bio::mapped_file::mapmode::readwrite;
bio::stream<bio::mapped_file_sink> out(params);
copy(strArray.begin(), strArray.end(), ostream_iterator<string>(out, "\n"));
}
There is a boost library specifically for exactly this. 有一个专门用于此的增强库。 It's called "boost interprocess". 它被称为“增强进程间”。
The documentation (with examples) is here: 文档(带示例)在这里:
http://www.boost.org/doc/libs/1_59_0/doc/html/interprocess.html http://www.boost.org/doc/libs/1_59_0/doc/html/interprocess.html
I know of no portable way to do memory-mapped files (though maybe boost has something, boost usually does...). 我知道没有可移植的方式来做内存映射文件(虽然可能有一些东西,提升通常会......)。 Are you using Linux? 你在用Linux吗? Then there are good mechanisms for writing high-performance stuff with memory-mapped files, eg mmap(2). 然后有很好的机制来编写具有内存映射文件的高性能内容,例如mmap(2)。 Those mechanisms are also partly portable to other major Unix OS:es. 这些机制也部分可移植到其他主要的Unix操作系统:es。 (Yes, Linux is Unix) For some applications, using threads (that of course share virtual memory space) is an alternative. (是的,Linux是Unix)对于某些应用程序,使用线程(当然共享虚拟内存空间)是另一种选择。 Then you do not have portability problems. 然后你没有可移植性问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.