title: Moving objects containing a running std::thread member
body: <p>How to correctly use the move semantic with running thread in object?</p>
  
  <p>Sample:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;vector&gt;
  
  struct A {
      std::string v_;
      std::thread t_;
  
      void start() {
          t_ = std::thread(&amp;A::threadProc, this);
      }
  
      void threadProc() {
          for(;;) {
              std::cout &lt;&lt; "foo-" &lt;&lt; v_ &lt;&lt; '\n';
              std::this_thread::sleep_for(std::chrono::seconds(5));
          }
      }
  };
  
  int main() {
      A m;
      {
          A a;
          a.v_ = "bar";
          a.start();
          m = std::move(a);
      }
      std::cout &lt;&lt; "v_ = " &lt;&lt; m.v_ &lt;&lt; '\n'; /* stdout is 'v_ = bar' as expected    */
                                            /* but v_ in thread proc was destroyed */
                                            /* stdout in thread proc is 'foo-'     */
      m.t_.join();
      return 0;
  }
  </code></pre>
  
  <p>I want to use class members after moving, but when I go out scope, class members are destroyed and std::thread is moved into new object as expected but it starting use destroyed members. </p>
  
  <p>It seems to me because of using <code>this</code> pointer in thread initialization.</p>
  
  <p>What is best practice in this case? </p>