Boost logo

Boost Users :

From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2007-03-08 11:59:36


Andrew Agno wrote:
> I'm trying to run the following program, which works under Linux, on an
> intel
> mac:
>
> #include <boost/interprocess/sync/named_condition.hpp>
> #include <boost/interprocess/sync/named_mutex.hpp>
> #include <boost/interprocess/sync/scoped_lock.hpp>
> #include <iostream>
>
> using namespace std;
> namespace bi = boost::interprocess;
>
> int main( int argc, char **argv )
> {
> cout << "Creating mutex" << endl;
> try
> {
> bi::named_mutex directoryMutex( bi::create_only,
> "MyVeryUniquelyNamedMutex" );
> cout << "Creating scoped lock" << endl;
> bi::scoped_lock<bi::named_mutex> lock( directoryMutex );
> cout << "All reqs met. Destroying mutex" << endl;
> bi::named_mutex::remove( "MyVeryUniquelyNamedMutex" );
> }
> catch( std::exception &e )
> {
> cout << "Problem encountered: " << e.what() << endl;
> }
>
> return 0;
> }

I don't have a MacOs to test the code and I didn't know people was
trying to use it with MacOS. But glad to know you are trying. Anyway,
you are erasing the mutex and the before the scoped_lock destructor is
called. That means that the destructor of the scoped_lock will try to
access to a mutex that's already destroyed. This does not seem correct.
The "remove" function should be after the mutex is used. Something like:

try
  {
     {
        bi::named_mutex directoryMutex( bi::create_only,
"MyVeryUniquelyNamedMutex" );
        cout << "Creating scoped lock" << endl;
        bi::scoped_lock<bi::named_mutex> lock( directoryMutex );
        cout << "All reqs met. Destroying mutex" << endl;
     }
     bi::named_mutex::remove( "MyVeryUniquelyNamedMutex" );
  }
  catch( std::exception &e )
  {
     cout << "Problem encountered: " << e.what() << endl;
  }

Unless, of course, that you need to destroy the mutex while it's been
used (something for which, I have no solution).

Regards,

Ion


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net