崩溃后仍会获取名为互斥量的升压进程间

发布于 2024-12-10 10:48:06 字数 1001 浏览 0 评论 0原文

我正在使用 boost::interpocess::scoped_lock 使用 named_mutextimeout;我正在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;
      }
     ....

我的问题如下:

  1. 如何确保名为互斥体的 boost::interprocess 被销毁? (那么如何查看跨进程共享的互斥锁以及如何销毁它们)
  2. 为什么获取互斥锁在 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:

  1. 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 )
  2. Why acquiring the mutex doesn't return after 150 microseconds? Is here any something wrong in the code below?

Thanks a lot

AFG

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

棒棒糖 2024-12-17 10:48:06

我找到了解决方案:我错过了调用以下代码来销毁互斥体

 boost::interprocess::named_mutex::remove( "MutexName" );

此代码进行了所有必要的清理。

I found the solution: I missed to call the following to destroy the mutex

 boost::interprocess::named_mutex::remove( "MutexName" );

This code makes all the necessary clean up.

<逆流佳人身旁 2024-12-17 10:48:06
boost::interprocess::named_mutex::remove( "MutexName" ); 

这应该是不正确的。
这也将为所有其他进程解锁互斥锁。

boost::interprocess::named_mutex::remove( "MutexName" ); 

This should not be correct.
This will unlock the mutex for all other processes, too.

森罗 2024-12-17 10:48:06

在 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.

热血少△年 2024-12-17 10:48:06

不要使用 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!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文