崩溃后仍会获取名为互斥量的升压进程间
我正在使用 boost::interpocess::scoped_lock
使用 named_mutex
和 timeout
;我正在Linux操作系统中运行。
在我的一次测试中,我发生了崩溃:从那时起,每次我尝试再次运行该应用程序时,它都会卡在我创建锁的位置;看起来互斥锁仍然以某种方式获取(没有可能使用它的进程正在运行)。
最重要的是,如果你看下面的代码,我预计在 150 微秒后,定时 scoped_lock
返回给我一个错误..但事实并非如此..它只是挂在那里。
#include <boost/interprocess/sync/named_mutex.hpp>
namespace bi = boost::interprocess;
bi::named_mutex m_mutex;
try{
boost::posix_time::ptime pt(
boost::posix_time::microsec_clock::local_time() ) ;
pt+= boost::posix_time::microseconds( 150 );
bi::scoped_lock< bi::named_mutex > lock( m_mutex, pt );
if( !lock.owns() ){
FATAL( "I didn't acquire the lock." );
return EXIT_FAILURE;
}
....
我的问题如下:
- 如何确保名为互斥体的
boost::interprocess
被销毁? (那么如何查看跨进程共享的互斥锁以及如何销毁它们) - 为什么获取互斥锁在 150 微秒后没有返回?下面的代码有什么问题吗?
非常感谢
AFG
I am using a boost::interpocess::scoped_lock
using a named_mutex
and a timeout
; I am running in Linux OS.
During one of my tests I had a crash: since then, everytime I try to run again the application, it gets stuck on the point where I created the lock; it looks like the mutex remained acquired some way ( no possible process using it is running ).
On top of that if you look at the code below I am expecting that after 150 microseconds, the timed scoped_lock
returns to give me an error..but this is not the case..it just hangs there.
#include <boost/interprocess/sync/named_mutex.hpp>
namespace bi = boost::interprocess;
bi::named_mutex m_mutex;
try{
boost::posix_time::ptime pt(
boost::posix_time::microsec_clock::local_time() ) ;
pt+= boost::posix_time::microseconds( 150 );
bi::scoped_lock< bi::named_mutex > lock( m_mutex, pt );
if( !lock.owns() ){
FATAL( "I didn't acquire the lock." );
return EXIT_FAILURE;
}
....
My questions are the following:
- How to make sure that
boost::interprocess
named mutex is destroyed? ( so how to see the shared mutex across the processes and how to destroy them ) - Why acquiring the mutex doesn't return after 150 microseconds? Is here any something wrong in the code below?
Thanks a lot
AFG
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了解决方案:我错过了调用以下代码来销毁互斥体
此代码进行了所有必要的清理。
I found the solution: I missed to call the following to destroy the mutex
This code makes all the necessary clean up.
这应该是不正确的。
这也将为所有其他进程解锁互斥锁。
This should not be correct.
This will unlock the mutex for all other processes, too.
在 unix 上崩溃时,命名互斥体不会释放,请尝试使用 boost::interprocess::file_lock 代替。
当崩溃发生时,锁被释放。
Named mutex will not release when crash on unix, try boost::interprocess::file_lock instead.
When crash happens, the lock is released.
不要使用 local_time() 函数,而是使用 universal_time():
boost::posix_time::ptime abs_time = boost::posix_time::microsec_clock::universal_time() + boost::posix_time::毫秒(150);
scoped_lock 储物柜(互斥锁,abs_time);
如果进程崩溃,您应该捕获崩溃信号并解锁named_mutex,或者使用一个线程作为计时器来检查死锁并解锁它。
使用boost::interprocess::file_lock会引入新问题,小心!!!
do not use local_time() function, instead of using universal_time():
boost::posix_time::ptime abs_time = boost::posix_time::microsec_clock::universal_time() + boost::posix_time::milliseconds(150);
scoped_lock locker(mutex, abs_time);
if process crash, you should capture crash signal and unlock the named_mutex or have a thread as a timer to check dead lock and unlock it.
using boost::interprocess::file_lock new problems will be introduced, carefully!!!