9

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?

ildjarn
  • 59,718
  • 8
  • 115
  • 201
Toby
  • 3,575
  • 12
  • 43
  • 64
  • I'm confused by your question and the inter-use of the terms "threads" and "processes". Which are you dealing with? threads? or processes? Named mutices typically have exceedingly marginal utility if you're only multithreaded within a single process. – Nathan Ernst Sep 26 '11 at 21:49
  • Ye sorry about that - I just got a lil bit confused while writing. Actually I have different processes. As you said, with only threads there would be no actual need for named mutexes. – Toby Sep 27 '11 at 08:07

3 Answers3

6

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().

Nathan Ernst
  • 4,300
  • 21
  • 37
  • So if I get it right, as long as I don't call remove() the resource will stay in the system ( even if the destructor is called ) - But that doesnt matter since the destructor tells the OS that the Resource is "unlocked" , so another process could lock it? Is that right so?? – Toby Sep 27 '11 at 07:45
  • I've not played with them much, myself, but I think if the number of processes using the mutex goes to zero, the resources will be freed. the man page for sem_close on my linux box says: DESCRIPTION sem_close() closes the named semaphore referred to by sem, allowing any resources that the system has allocated to the calling process for this semaphore to be freed. – Nathan Ernst Sep 27 '11 at 17:05
  • 12
    Okay well I did a little bit of resarch and trial and error yesterday. The resource for the mutex is located in the shared memory ( /dev/shm/ ). After it is created it will stay there until you call remove() or the system reboots. ( Or you do anything else to clear the shared memory, e.g. erasing it by yourself ). The object itself contains information about wether it's locked or not. When the mutex gets destroyed it gets just unlocked but not erased! So the only problem could be that the program crashes hard and can't call the destructor and the object stays locked. – Toby Sep 28 '11 at 07:17
  • But if that happens I assume that there is something way more severe wrong so I actually don't worry about the mutex in the first way! So I'd say -> Problem solved! Thank you! – Toby Sep 28 '11 at 07:18
0

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.

thiton
  • 34,333
  • 3
  • 63
  • 96
-1

According to the boost manual:

  1. This is a mutex with a global name, so it can be found from different processes. However, this mutex can't be placed in shared memory. @Toby
  2. 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().

I think the above should have addressed your questions.

Charlie
  • 43
  • 5