title: Using a mutex in conjunction with shared_ptr to protect data shared across copies
body: <p>I have a class whose copies share the same data via <code>shared_ptr</code>. Since that data is going to be modified by different threads I'm going to use a mutex in order to protect it, however, it is my understanding that the mutex object has to be the same across different copies in order to work, ignoring the fact that mutexes are not actually copyable.</p>
  
  <p>Therefore I intend to put the mutex into a <code>shared_ptr</code> as well. Like so: </p>
  
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  
  #include &lt;mutex&gt;
  #include &lt;memory&gt;
  #include &lt;vector&gt;
  
  class test {
  public:
    auto some_action(int x) -&gt; void {
      std::scoped_lock(*m_store_mutex);
  
      m_shared_store-&gt;push_back(x);
    }
  
  private:
    std::shared_ptr&lt;std::mutex&gt; m_store_mutex { std::make_shared&lt;std::mutex&gt;() };
    std::shared_ptr&lt;std::vector&lt;int&gt;&gt; m_shared_store { std::make_shared&lt;std::vector&lt;int&gt;&gt;() };
  };
  
  </code></pre>
  
  <p>Is this approach valid? It seems to be working but I'd like to be sure.</p>
  
  <p>P.S.</p>
  
  <p><a href="https://stackoverflow.com/questions/9483657/boostshared-ptr-boostmutex-and-copy-constructor">This</a> question is fairly similar to mine, but I do not feel like the answers are quite specific enough.</p>