【问题标题】:Why is boost's interprocess mutex not robust on posix?为什么 boost 的进程间互斥锁在 posix 上不健壮?
【发布时间】:2016-11-06 17:41:21
【问题描述】:

我对 boost 很陌生,只是想了解其中的一小部分 - 进程间互斥锁。

AFAIU,在使用文件锁的跨平台实现中有一个强大的互斥体模拟:http://www.boost.org/doc/libs/1_62_0/boost/interprocess/detail/robust_emulation.hpp

这是为了模拟 posix 标准中可用的健壮互斥锁,但通用方法仅在平台特定版本不可用时使用。

#if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
   #include <boost/interprocess/sync/posix/mutex.hpp>
   #define BOOST_INTERPROCESS_USE_POSIX
//Experimental...
#elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
   #include <boost/interprocess/sync/windows/mutex.hpp>
   #define BOOST_INTERPROCESS_USE_WINDOWS
#elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
   #include <boost/interprocess/sync/spin/mutex.hpp>
   #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION

但是在实际支持健壮互斥锁的 posix 系统上(我猜这取决于内核版本),健壮互斥锁实际上并没有启用。 http://www.boost.org/doc/libs/1_62_0/boost/interprocess/sync/posix/pthread_helpers.hpp

所以我不太了解这里的逻辑,posix 强大的互斥锁实现是否存在严重的性能损失,所以我们不想使用它?即便如此,至少应该有一个选项可以选择启用此功能。

为了解决这个问题,因为我是菜鸟,有没有办法让通用实现在 posix 实现上可用,以便我可以利用健壮性?

【问题讨论】:

    标签: c++ boost boost-mutex


    【解决方案1】:

    我刚刚在文件 boost/interprocess/sync/posix/pthread_helpers.hpp 中查看了用于交叉互斥的包装类的 boost 实现。 我看到 PTHREAD_MUTEX_ROBUST 不见了。 请添加如下 ROBUST 属性并尝试。

    mutexattr_wrapper(bool recursive = false)
     {
             if(pthread_mutexattr_init(&m_attr)!=0 ||
                pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0 ||
                 (recursive &&
                  pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE)!= 0 ) ||
                  pthread_mutexattr_setrobust(&m_attr, PTHREAD_MUTEX_ROBUST)!= 0)
                throw interprocess_exception("pthread_mutexattr_xxxx failed");
    } 
    

    【讨论】:

      猜你喜欢
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      相关资源
      最近更新 更多