boost interprocess named mutex remains acquired after a crash
Asked Answered
S

4

9

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

Sadie answered 18/10, 2011 at 14:0 Comment(0)
S
6

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.

Sadie answered 19/10, 2011 at 10:5 Comment(0)
T
2
boost::interprocess::named_mutex::remove( "MutexName" ); 

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

Terminate answered 9/4, 2015 at 11:36 Comment(0)
S
2

Named mutex will not release when crash on unix, try boost::interprocess::file_lock instead. When crash happens, the lock is released.

Salomo answered 18/8, 2015 at 1:50 Comment(1)
In my case it remained acquired on Windows tooSouthwest
H
2

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!!!

Ham answered 24/10, 2017 at 6:34 Comment(1)
you can use macro to change the named_mutex internal implementation to windows mutex or posix mutex, but it also have some problem, you can view the boost/interprocess gitbub, there is something updated, but latest boost version not merge it, you should change the interprocess source code. or you can only use OS APIHam

© 2022 - 2024 — McMap. All rights reserved.