boost managed_shared_memory find()方法永远卡在互斥锁上

boost managed_shared_memory find()方法永远卡在互斥锁上,boost,shared-memory,Boost,Shared Memory,我有以下程序,这是它应该做的: 打开/创建共享内存段 对该托管共享内存段调用find()以查找对象 如果未找到,请实例化它 如果找到,只需转储内容 我用以下方法测试它: 在一个窗口中运行此程序的一个实例 它创建共享mem段,并实例化对象 永远等待(不确定是否需要,只是添加了它以确保linux不会在程序退出时释放共享内存) 运行同一程序的另一个实例,并期望它找到对象(这就是它卡住的地方) 类数据存储{ 公众: 数据存储(uint32\t id,常量字符*名称,布尔安全){ id=id; st

我有以下程序,这是它应该做的:

  • 打开/创建共享内存段
  • 对该托管共享内存段调用find()以查找对象
  • 如果未找到,请实例化它
  • 如果找到,只需转储内容
我用以下方法测试它:

  • 在一个窗口中运行此程序的一个实例
  • 它创建共享mem段,并实例化对象
  • 永远等待(不确定是否需要,只是添加了它以确保linux不会在程序退出时释放共享内存)
  • 运行同一程序的另一个实例,并期望它找到对象(这就是它卡住的地方)

    类数据存储{
    公众:
    数据存储(uint32\t id,常量字符*名称,布尔安全){
    id=id;
    strncpy(名称、名称、尺寸(名称));
    安全=安全;
    }
    ~data_store(){}
    uint32_t id(void)const{return id_;}
    const char*name(void)const{返回名称}
    私人:
    字符名_u32];
    布尔安全;
    uint32\u t id\u;
    };
    int
    主要()
    {
    托管共享内存*test\u shmseg;
    数据存储*ds;
    试一试{
    test_shmseg=新的受管共享内存(打开或创建“seg2”,2048);
    }捕获(std::exception ex){
    标准:cerr
    
    • 永远等待(不确定是否需要,只是添加了它以确保linux不会在程序退出时释放共享内存)
    不,这不是必需的。共享内存是共享的。除非您显式地
    remove()
    It,否则它将保留

    复习 您至少有一个不一致:对象的名称是
    “Datastore”
    “Datastore”
    -请确保拼写匹配

    除此之外,我认为

    • 您可能不希望使用(无意中?)正在使用的“数组样式”分配
    • 您最好使用
      find_或_-construct
      ,这样可以消除潜在的竞争条件(分别在查找和创建新实例之间的检查时间与使用时间窗口)
    除此之外,我看不出挂起的直接原因。也许您可以尝试手动删除共享对象一次,然后使用以下简化程序重新测试:

    #include <boost/interprocess/managed_shared_memory.hpp>
    #include <iostream>
    #include <cassert>
    namespace bip = boost::interprocess;
    
    class data_store {
      public:
        data_store(uint32_t id, const char *name, bool safe) :
            id_(id), safe_(safe)
        {
            id_ = id;
            assert(name && strlen(name) < (sizeof(name_)-1));
            strncpy(name_, name, sizeof(name_));
            safe_ = safe;
        }
    
        uint32_t   id()    const { return id_; }
        const char *name() const { return name_; }
    
      private:
        char name_[32] = {0};
        uint32_t id_;
        bool safe_;
    };
    
    int main () try {
        bip::managed_shared_memory seg(bip::open_or_create, "seg2", 2048);
        data_store ds = *seg.find_or_construct<data_store>("DataStore")(1, "ds", true);
        std::cout << "Free size " << seg.get_free_memory() << std::endl;    
        std::cout << "Data store name " << ds.name() << std::endl;
    } catch (std::exception ex) {
        std::cerr << ex.what() << '\n';
    }
    

    谢谢..这很有效..我删除了现有的段,并将对象的名称固定在数据存储/数据存储之间,使其与工作状态一致。如果对象不存在,我希望不会像那样被卡住。如果删除段是关键步骤,则可能意味着进程因嵌入互斥锁而崩溃ed.没有真正的(便携式)解决方案:
    (gdb) bt
    #0  0x00007ffff7bcd42d in __lll_lock_wait () from /lib64/libpthread.so.0
    #1  0x00007ffff7bc8de6 in _L_lock_870 () from /lib64/libpthread.so.0
    #2  0x00007ffff7bc8cdf in pthread_mutex_lock () from /lib64/libpthread.so.0
    #3  0x0000000000403d6d in boost::interprocess::ipcdetail::posix_recursive_mutex::lock (this=0x7ffff7ff8070)
        at /usr/include/boost/interprocess/sync/posix/recursive_mutex.hpp:90
    #4  0x0000000000403e52 in boost::interprocess::interprocess_recursive_mutex::lock (this=0x7ffff7ff8070)
        at /usr/include/boost/interprocess/sync/interprocess_recursive_mutex.hpp:163
    #5  0x0000000000408457 in boost::interprocess::scoped_lock<boost::interprocess::interprocess_recursive_mutex>::lock
        (this=0x7fffffffdf30) at /usr/include/boost/interprocess/sync/scoped_lock.hpp:284
    #6  0x00000000004073b0 in boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::interprocess::iset_index>::priv_get_lock (this=0x7ffff7ff8010, use_lock=true)
        at /usr/include/boost/interprocess/segment_manager.hpp:1315
    #7  0x00000000004069ea in boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::interprocess::iset_index>::priv_generic_find<char> (this=0x7ffff7ff8010, name=0x41c4a5 "Datastore", index=..., table=...,
        length=@0x7fffffffe098: 0, is_intrusive=..., use_lock=true)
        at /usr/include/boost/interprocess/segment_manager.hpp:844
    #8  0x0000000000405f1e in boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::interprocess::iset_index>::priv_find_impl<data_store> (this=0x7ffff7ff8010, name=0x41c4a5 "Datastore", lock=true)
        at /usr/include/boost/interprocess/segment_manager.hpp:724
    #9  0x0000000000405309 in boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::interprocess::iset_index>::find<data_store> (this=0x7ffff7ff8010, name=...)
        at /usr/include/boost/interprocess/segment_manager.hpp:434
    #10 0x00000000004044ea in boost::interprocess::ipcdetail::basic_managed_memory_impl<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::interprocess::iset_index, 16ul>::find<data_store> (this=0x63ec20, name=...)
        at /usr/include/boost/interprocess/detail/managed_memory_impl.hpp:346
    #11 0x00000000004040f7 in boost::interprocess::basic_managed_shared_memory<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void, long, unsigned long, 0ul>, 0ul>, boost::int---Type <return> to continue, or q <return> to quit---
    erprocess::iset_index>::find<data_store> (this=0x63ec20, name=...)
        at /usr/include/boost/interprocess/managed_shared_memory.hpp:212
    
    #include <boost/interprocess/managed_shared_memory.hpp>
    #include <iostream>
    #include <cassert>
    namespace bip = boost::interprocess;
    
    class data_store {
      public:
        data_store(uint32_t id, const char *name, bool safe) :
            id_(id), safe_(safe)
        {
            id_ = id;
            assert(name && strlen(name) < (sizeof(name_)-1));
            strncpy(name_, name, sizeof(name_));
            safe_ = safe;
        }
    
        uint32_t   id()    const { return id_; }
        const char *name() const { return name_; }
    
      private:
        char name_[32] = {0};
        uint32_t id_;
        bool safe_;
    };
    
    int main () try {
        bip::managed_shared_memory seg(bip::open_or_create, "seg2", 2048);
        data_store ds = *seg.find_or_construct<data_store>("DataStore")(1, "ds", true);
        std::cout << "Free size " << seg.get_free_memory() << std::endl;    
        std::cout << "Data store name " << ds.name() << std::endl;
    } catch (std::exception ex) {
        std::cerr << ex.what() << '\n';
    }
    
    Free size 1712
    Data store name ds
    -rw-r--r-- 1 2001 2000 2.0K Mar  5 12:26 seg2
    Free size 1712
    Data store name ds