Boost named_mutex and remove() command

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 00:57:38

问题


I have a class which can be created by multiple threads. But at one function the code needs to be protected, so I decided to use the boost interprocess mutex. Every class creates or opens the same Mutex in it's constructor:

MyClass::MyClass()
{
       boost::interprocess::named_mutex m_Lock(
                 boost::interprocess::open_or_create, "myLock" );
}

So now there comes the point where the critical code part is called:

int MyClass::MyFunction()
{
       boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(
                  m_Lock, boost::interprocess::try_to_lock);
       if(!lock)
       {
           return -1;
       }
       // else do some stuff here 
}

To clean up after the function ( and like its described on the boost page ) I use the remove command in my class destructor:

MyClass::~MyClass()
{
       boost::interprocess::named_mutex::remove("myLock");
}

Actually all this code works fine, but there is one concern I have:

As it is said in the description of the remove command :

Erases a named mutex from the system. Returns false on error. Never throws.

So that means the remove command just erases the Mutex out of the system - even if another thread has just locked it ( I tried this case already - it isn't locked then anymore ). So my problem is the following: For example I have 3 Threads ( A, B and C ) - now the following happens:

  1. Process A creates an Instance of the class, calls the function and locks it
  2. Process B creates an Instances of the class, calls the function but can't access code ( then waits e.g. )
  3. Process A finishes with the protected code and it gets unlocked
  4. Process B gains access to the protected code and locks it
  5. Process A deletes the Instance of the class -> the remove command is called
  6. Process C creates an Instance of the class, calls the function and can access the code since the remove command erased the Mutex --> Error!

So now someone may say " Then don't call remove! " - Well is that possible? I mean since the named_mutex writes to the system I doubt it is erased without an explicit call, even if the program ends. Anyone has some help?


回答1:


From the boost docs, the remove call, is unnecessary. The destructor of named_mutex will automatically take care indicating to the OS that the process no longer needs the resource. You're probably fine with just relying upon the built-in behavior of the destructor for cleanup.

If you explicitly call remove, you'll likely cause any other processes or threads attempting to use the named mutex to fail on any operations on the mutex. Depending on how your usage is orchestrated, this could either cause data races or crashing/exceptions being thrown in other processes.

~named_mutex();

Destroys *this and indicates that the calling process is finished using the resource. The destructor function will deallocate any system resources allocated by the system for use by this process for this resource. The resource can still be opened again calling the open constructor overload. To erase the resource from the system use remove().




回答2:


You probably need a shared usage counter for the mutex. Lock the mutex in the destructor, decrement it, and if it's zero after the decrement, deallocate the still locked mutex. You will prevent your current race condition by that.



来源:https://stackoverflow.com/questions/7555253/boost-named-mutex-and-remove-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!