title: Is using the class-wrapper for thread-safe access to the object conform to C++11?
body: <p>Can I use this class-wrapper for thread-safe access to the object, and desired behavior conforms to C++11?</p>
  
  <p>Main accent to the strings:</p>
  
  <pre><code>T* operator-&gt;() {
  </code></pre>
  
  <p>and</p>
  
  <pre><code>T&amp; operator*() {
  </code></pre>
  
  <p>Note, I know that here optimal to use std::atomic&lt;> for the integer(int), but in this code instead of int we can use any other object.</p>
  
  <p>Version 2.0 by using <a href="http://en.wikibooks.org/wiki/More_C++_Idioms/Execute-Around_Pointer" rel="nofollow">execute-around pointer idiom</a>:</p>
  
  <pre><code>#include&lt;thread&gt;
  #include&lt;mutex&gt;
  #include&lt;memory&gt;
  #include&lt;iostream&gt;
  #include&lt;vector&gt;
  
  template&lt;typename T&gt;
  class safe_obj {
      T obj;
      mutable std::mutex mtx;
      safe_obj(safe_obj&lt;T&gt; &amp;) {}
      safe_obj&lt;T&gt;&amp; operator=(safe_obj&lt;T&gt; &amp;) {}
  
      class T_exclusive_lock {
           std::unique_lock&lt;std::mutex&gt; xlock;
      public:
           T*const self;
           T_exclusive_lock(T * const s, std::mutex&amp; _mtx)
               :self(s), xlock(_mtx) {}
  
          T* operator -&gt; () const {return self;}
          operator T&amp;()  {return *self;}
  
          friend std::ostream&amp; operator &lt;&lt; (std::ostream &amp;stream, T_exclusive_lock &amp;obj) {
              stream &lt;&lt; obj;
              return stream;
          }
      };
  
  public:
      template&lt;typename  Types&gt;
      safe_obj(Types  args) : obj(args ) 
      { }
  
      T_exclusive_lock operator-&gt;() {
          return T_exclusive_lock(&amp;obj, mtx);
      }    
  
      T_exclusive_lock* operator*() {
          return &amp;T_exclusive_lock(&amp;obj, mtx);
      }       
  };
  
  int main() {
      safe_obj&lt;std::shared_ptr&lt;int&gt; &gt; safe_int( std::make_shared&lt;int&gt;(10) );   
  
      auto lambda = [&amp;safe_int]() {
          std::cout &lt;&lt; safe_int-&gt;use_count() &lt;&lt; std::endl;    // is that thread-safe? 
          std::cout &lt;&lt; *safe_int &lt;&lt; std::endl;    // is that thread-safe? 
          std::cout &lt;&lt; *safe_int-&gt;get() &lt;&lt; std::endl;    // is that thread-safe? 
      };
  
      std::vector&lt;std::thread&gt; thr_grp;
      for(size_t i = 0; i &lt; 10; ++i) thr_grp.emplace_back(std::thread(lambda));
      for(auto &amp;i : thr_grp) i.join();
  
      int b; std::cin &gt;&gt; b;
      return 0;
  }
  </code></pre>