Using memory mapped file to save - is volatile required?

I need a tag persistence uint64_tto restart.

To do this, I use the boost::interprocess::mapped_regionfile for the memory card that I create in the same process:

bip::file_mapping file(filename.c_str(), bip::read_write);
auto region = std::make_unique<bip::mapped_region>(file, bip::read_write);

Then I listed the address in my type uint64_t

using Tag = uint64_t;
Tag& curr_ = *reinterpret_cast<Tag*>(region->get_address());

Now I can add the post-increment tag, getting the "next tag", and the results are saved through reloads

Tag next = curr_++;

Please note that this file is written to and from only by this process. The goal is to ensure consistency.

Question:

Is mine Tag& curr_, being non-volatile and doing I / O to a memory mapped area, undefined -behavior?

To be true, does my code require a keyword volatile?

:

#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <sys/stat.h>
#include <fstream>
#include <cstdint>
#include <memory>
#include <iostream>

namespace bip = boost::interprocess;

using Tag = uint64_t;

Tag& map_tag(const std::string& filename,
             std::unique_ptr<bip::mapped_region>& region)
{
    struct stat buffer;
    if (stat(filename.c_str(), &buffer) != 0)
    {
        std::filebuf fbuf;
        fbuf.open(filename.c_str(), std::ios_base::in | 
                                    std::ios_base::out | 
                                    std::ios_base::trunc | 
                                    std::ios_base::binary);

        Tag tag = 1;
        fbuf.sputn((char*)&tag, sizeof(Tag));
    }

    bip::file_mapping file(filename.c_str(), bip::read_write);

    // map the whole file with read-write permissions in this process
    region = std::make_unique<bip::mapped_region>(file, bip::read_write);

    return *reinterpret_cast<Tag*>(region->get_address());
}

class TagBroker
{
public:
    TagBroker(const std::string& filename)
        : curr_(map_tag(filename, region_))
    {}

    Tag next()
    {
        return curr_++;
    }

private:
    std::unique_ptr<bip::mapped_region> region_;
    Tag& curr_;
};

int main()
{
    TagBroker broker("/tmp/tags.bin");

    Tag tag = broker.next();

    std::cout << tag << '\n';
    return 0;
}

:

.

$ ./a.out
1
$ ./a.out
2
$ ./a.out
3
$ ./a.out
4

, , / Tag& curr_, , , undefined .

+4
1

.

Boost interprocess/mapped_region.hpp mmap, .

volatile, , ( ) .

( , , volatile . , , ​​ .)

+2

Source: https://habr.com/ru/post/1683203/


All Articles