3

I have the following code:

void Func()
{
    boost::interprocess::named_mutex someMutex(boost::interprocess::open_or_create, "MyMutex");
    boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(someMutex);

    // ... some stuff happens here
}

and many applications are calling Func. If there is a crash in Func, the mutex isn't released, so I'm considering removing the mutex, by calling boost::interprocess::named_mutex::remove("MyMutex"), if I detect any of my app crashed.

Due to special circumstance, it's actually safe for two threads or processes to enter the protected area at the same time, because the content of Func() only does things for the first application that runs.

I have two questions:

  1. is what I'm planning to do a good idea?
  2. What happens if:
    • Process A opens the mutex and locks it (the mutex had been created previously)
    • Process B dies. We detect that and remove the mutex
    • Process C creates the mutex and locks it
    • Process A finishes running Func(), and the scoped_lock destructor releases the mutex
    • Process C finishes running Func(), and the scoped_lock destructor releases the mutex

now do I have a "double released" named_mutex, or did Process A just didn't do anything in the scoped_lock destructor because the named_mutex it locked had already been removed?

ildjarn
  • 59,718
  • 8
  • 115
  • 201
Warpin
  • 6,646
  • 11
  • 49
  • 73
  • See this question and answer: http://stackoverflow.com/questions/7555253/boost-named-mutex-and-remove-command. The gist is, you probably should not call remove. – A.E. Drew Mar 05 '15 at 00:38

0 Answers0