-->

提振间managed_mapped_file找到失败(boost interprocess mana

2019-10-21 19:53发布

我想分享横跨在使用升压进程间的结构。

我定义映射文件,因为我是有它锁定问题以空互斥,我不介意做同步自己。

什么我有,虽然是找到对象的问题。

我有以下声明:

typedef boost::interprocess::basic_managed_mapped_file
    < char,
    boost::interprocess::rbtree_best_fit<boost::interprocess::null_mutex_family,boost::interprocess::offset_ptr<void>>,
    boost::interprocess::flat_map_index>
    my_mapped_file;

在方法A,我做的:

m_managedMappedFile.reset(new my_mapped_file(bip::open_or_create, filename, filesize));
auto hdr = m_managedMappedFile->find_or_construct<Foo>(bip::unique_instance)();
auto x = m_managedMappedFile->find<Foo>(bip::unique_instance);

其中一期工程为我所期望的,例如,它找到的对象。 现在,在流程B:

    m_managedMappedFile.reset(new my_mapped_file(bip::open_only, filename));
    auto ret = m_managedMappedFile->find<Foo>(bip::unique_instance);

出于某种原因,查找方法返回过程B.空我知道我必须做一些愚蠢的,但不能弄明白。

任何人都可以帮忙吗?

Answer 1:

你不应该绕过默认的锁定机制bip::managed_mapped_file指标。

看看你是否可以运行成功如下:

#include <iostream>
#include <boost/interprocess/managed_mapped_file.hpp>

namespace bip = boost::interprocess;

struct X {
    int i;
};

int main()
{
    {
        bip::managed_mapped_file f(bip::open_or_create, "/tmp/mmf.bin", 1ul << 20);

        if (!f.find<X>(bip::unique_instance).first) {
            auto xp = f.find_or_construct<X>(bip::unique_instance)();

            assert(xp);
            xp->i = 42;
        }
    }

    {
        bip::managed_mapped_file f(bip::open_only, "/tmp/mmf.bin");
        auto xp = f.find<X>(bip::unique_instance).first;

        if (xp)
            std::cout << "value: " << xp->i++ << "\n";
    }
}

这应该打印42在第一次运行(或文件后,已重新创建),并在以后每次运行越来越多。

我要看看后面的实施unique_instance_t*段经理的过载,但我怀疑他们可能无法工作,因为互斥政策清零 。 这仅仅是一种预感,虽然,此刻。

我会专注于找出你为什么不能让进程间managed_mapped_file在默认配置下工作,你的平台和安装上。



文章来源: boost interprocess managed_mapped_file find failing