为什么 boost 的进程间互斥锁在 posix 上不健壮?
Why is boost's interprocess mutex not robust on posix?
我是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实现上可用,以便我可以利用鲁棒性?
我刚刚在boost/interprocess/sync/posix/pthread_helpers.hpp文件中查看了用于交叉互斥锁的包装类的boost实现。我看到PTHREAD_MUTEX_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");
}
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 使用Boost async_read和POSIX::stream_descriptor从键盘读取
- 使用Posix时间的Boost线程超时
- 如何使用 Boost 获取 POSIX 时区
- 使用Boost ASIO和Boost posix时间在WinXP 32下对传入的串行数据进行时间戳
- C++:我应该使用Boost.Date_Time Posix时间还是本地时间
- 为什么 boost 的进程间互斥锁在 posix 上不健壮?
- POSIX cksum and Boost.CRC
- POSIX equivalent of boost::thread::hardware_concurrency
- Visual Studio 2012中Boost posix日期和时间的编译错误
- 使用 Boost::asio posix stream_descriptor读取 ftrace 管道