title: Boost Interprocess named_mutex as a class member
body: <p>I have a class designed as a wrapper around some boost shared memory. When I try to put a named_mutex as a class member and initialize it in the constructor, I get compilation errors. Here is the relevant code:</p>
  
  <pre><code>   template&lt; class T &gt; class SharedMemoryVal
     {
        public:
           SharedMemoryVal( const std::string &amp;name, bool readOnly );
  
           SharedMemoryVal( const SharedMemoryVal&amp; other )
           {
              name = other.name;
              mutexName = other.mutexName;
              permissions = other.permissions;
              shm = other.shm;
              mutex = boost::interprocess::named_upgradable_mutex( boost::interprocess::open_or_create, mutexName.c_str() );
           }
  
           const SharedMemoryVal&amp; operator=( const SharedMemoryVal&amp; other )
           {
              if( this == &amp;other )
                 return *this;
  
              name = other.name;
              mutexName = other.mutexName;
              permissions = other.permissions;
              shm = other.shm;
              mutex = boost::interprocess::named_upgradable_mutex( boost::interprocess::open_or_create, mutexName.c_str() );
           }
  
           int8_t GetVal( T *val );
  
           int8_t SetVal( T val );
  
           virtual ~SharedMemoryVal();
  
        private:
           std::string name;
           std::string mutexName;
           boost::interprocess::mode_t permissions;
           boost::interprocess::shared_memory_object shm;
           mutable boost::interprocess::named_upgradable_mutex mutex;
     };
  
     using namespace boost::interprocess;
  
     /**
      * Construct new shared memory data value
      * @param[in] name - Name of shared memory segment
      * @param[in] readOnly
      */
     template&lt; class T &gt; SharedMemoryVal&lt; T &gt;::SharedMemoryVal( const std::string &amp;name, bool readOnly )
     {
        // Set name
        this-&gt;name = name;
  
        // Create mutex
        mutexName = this-&gt;name.append( "_mutex" );
  
        /* GETTING AN ERROR HERE */
       /*  error: \u2018boost::interprocess::named_upgradable_mutex::named_upgradable_mutex()\u2019 is private*/
        mutex = named_upgradable_mutex( open_or_create, mutexName.c_str() );
  
        //Create a shared memory object.
        if( readOnly )
        {
           permissions = read_only;
           shm = shared_memory_object( open_only, name.c_str(), read_only );
        }
        else
        {
           permissions = read_write;
           shm = shared_memory_object( create_only, name.c_str(), read_write );
  
           // Set size
           shm.truncate( sizeof(T) );
  
        }
     }
  
     template&lt; class T &gt; SharedMemoryVal&lt; T &gt;::~SharedMemoryVal()
     {
        shared_memory_object::remove( name.c_str() );
        named_upgradable_mutex::remove( mutexName.c_str() );
     }
     /**
      * Return current data value
      * @return current data value
      */
     template&lt; class T &gt; int8_t SharedMemoryVal&lt; T &gt;::GetVal( T *val )
     {
        int8_t retVal = -1;
        //Map the whole shared memory in this process
        mapped_region region( shm, permissions );
  
        // Get data pointer
        T *data = static_cast&lt; T * &gt;( region.get_address() );
  
        // Lock
        {
           // This will only lock if someone else is writing.
           // Multiple processes can read at the same time
           sharable_lock&lt; named_upgradable_mutex &gt; lock( mutex );
           // If there wasn't a problem getting the data
           if( data != NULL )
           {
              *val = *data;
              retVal = 0;
           }
        }
        return retVal;
     }
     /**
      * Set shared memory data value
      * @param[in] val - value to set
      */
     template&lt; class T &gt; int8_t SharedMemoryVal&lt; T &gt;::SetVal( T val )
     {
        int8_t retVal = -1;
        //Map the whole shared memory in this process
        mapped_region region( shm, permissions );
  
        // Get data pointer
        T *data = static_cast&lt; T * &gt;( region.get_address() );
  
        // Do lock
        {
           // Lock will always be unlocked at the end of this block
           scoped_lock&lt; named_upgradable_mutex &gt; lock( mutex );
  
           // If we got the data OK
           if( data != NULL )
           {
              *data = val;
              retVal = 0;
           }
        }
  
        return retVal;
     }
  </code></pre>
  
  <p>I'm getting an error in the constructor where I create a lock object, but I can't figure out why. I read that I must define my own copy constructor and assignment operator, and I've done that. Any ideas?</p>