c++ - 为什么 boost::interprocess::managed_shared_memory throw a boost::interprocess_exception upon construction?

标签 c++ linux boost g++ boost-interprocess

在下面的代码中,我正在尝试初始化managed_shared_memory对象。调用构造函数时,我看到以下错误消息-
抛出“boost::interprocess::interprocess_exception”的实例后调用终止
what():boost::interprocess_exception::library_error
弃用

为什么会抛出此异常?
我在ubuntu 16.04 linux操作系统上运行此程序,使用g++ 9.3.0编译了程序。 Boost版本1.58.0

struct test_obj {
    size_t x;
    size_t y;
    uint8_t buf[32];
    bool is_valid;
};

class shm_wrapper {
        public:
            shm_wrapper() : m_shm(
                                boost::interprocess::open_or_create, 
                                "my_shm",
                                sizeof(test_obj) )
            {};
        private:
        boost::interprocess::managed_shared_memory m_shm;
};

最佳答案

它中止,因为段管理器控制块的大小不足。sizeof(test_obj)仅为56个字节(在我的系统上)。
如果将段设为10 KiB,它将报告有效使用的224个字节:
Live On Coliru

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

namespace bip = boost::interprocess;

#ifdef COLIRU
    using segment_type = bip::managed_mapped_file;
#else
    using segment_type = bip::managed_shared_memory;
#endif

static constexpr size_t SegmentSize = 10 * 1024;

struct test_obj {
    size_t x;
    size_t y;
    uint8_t buf[32];
    bool is_valid;
};

class shm_wrapper {
  public:
    shm_wrapper() : m_shm(bip::open_or_create, "my_shm", SegmentSize){};

    size_t free() const { return m_shm.get_free_memory(); }

  private:
    segment_type m_shm;
};

#include <iostream>
int main() {
    std::cout << sizeof(test_obj) << std::endl;
    shm_wrapper w;
    std::cout << w.free() << "\n";
    std::cout << "Effectively used:" << (SegmentSize - w.free()) << "\n";
}
版画
56
10016
Effectively used:224
概要
也许您不想要具有动态分配功能的部门经理。在这种情况下,请改为查看 shared_memory_object
奖金:使用映射区域的示例
要将“哑对象”存储在固定大小的区域中,则不需要段管理器。您将在mapped_region(或shared_memory_object)上使用file_mapping
这是另一个示例 Live On Coliru
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
namespace bip = boost::interprocess;

struct test_obj {
    size_t x;
    size_t y;
    uint8_t buf[32];
    bool is_valid;
};

#ifdef COLIRU
    #include <fstream>
    using mapping_type = bip::file_mapping;
#else
    using mapping_type = bip::shared_memory_object;
#endif

template <typename T>
class shm_wrapper {
    static_assert(std::is_trivial_v<T>);
    static_assert(std::is_standard_layout_v<T>);

#ifdef COLIRU // file mappings require more work to cater for the storage
    struct backing_t { } backing;
    backing_t ensure_file(std::string name, size_t size) {
        std::filebuf fbuf;
        fbuf.open(name, std::ios::in | std::ios::out | std::ios::app | std::ios::binary);
        //set the size, sparsely
        fbuf.pubseekoff(size-1, std::ios_base::beg);
        fbuf.sputc(0);
        fbuf.close();
        return {};
    }
  public:
    shm_wrapper()
        : backing { ensure_file("my_shm", sizeof(T)) },
          m_mappable("my_shm", bip::read_write),
          m_reg(m_mappable, bip::read_write, 0, sizeof(T))
    { }
#else
  public:
    shm_wrapper()
        : m_mappable(bip::open_or_create, "my_shm", bip::read_write),
          m_reg(m_mappable, bip::read_write, 0, sizeof(T))
    {
        m_mappable.truncate(sizeof(T));
    }
#endif

    T& get()             { return *static_cast<T*>(m_reg.get_address()); }
    T const& get() const { return *static_cast<T const*>(m_reg.get_address()); }

    auto size() const { return m_reg.get_size(); } 
    auto flush()      { return m_reg.flush();    } 

  private:
    mapping_type m_mappable;
    bip::mapped_region m_reg;
};

int main() {
    shm_wrapper<test_obj> w;
    std::cout << "x:" << w.get().x << "\n";
    w.get().x ^= 0xa7;
    return w.flush()? 0 : 1;
}
连续运行4倍时打印:
x:0
x:167
x:0
x:167

关于c++ - 为什么 boost::interprocess::managed_shared_memory throw a boost::interprocess_exception upon construction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62748438/

相关文章:

boost - ranges-v3/排序困惑

c++ - 使用不同的符号表重新计算 Boost Spirit 解析的属性的最有效方法是什么?

c++ - 访问 std::pair 数组的元素时出错

STL - STL count_if 的标准谓词

c++ - 我可以在类外初始化一个 `constexpr static` 成员吗?

c++ - Lambda 语法或 gcc 错误的最后一刻更改?

linux - 如何在 shell 中使用包含数字属性的属性文件

linux - 在多个线程中从同一个 UDP 套接字发送

带有 wait(NULL) 的 C 程序没有像它应该的那样结束

c++ - std::back_insert_iterator 可以用在 std::ostream 上吗?