c++ boost write memory mapped file

2019-04-01 19:01发布

问题:

I am searching for fast writing a file using C++ and boost library. 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.

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.

回答1:

You could use Boost Iostreams mapped_file{_sink,_source} for this.

Although Boost Interprocess does use mapped files, but you'd be better off using IOstreams for this kind of raw access.

See http://www.boost.org/doc/libs/1_50_0/libs/iostreams/doc/classes/mapped_file.html

Live On Coliru

#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"));
}


回答2:

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



回答3:

I know of no portable way to do memory-mapped files (though maybe boost has something, boost usually does...). Are you using Linux? Then there are good mechanisms for writing high-performance stuff with memory-mapped files, e.g. mmap(2). Those mechanisms are also partly portable to other major Unix OS:es. (Yes, Linux is Unix) For some applications, using threads (that of course share virtual memory space) is an alternative. Then you do not have portability problems.