title: std::thread as member of class executing random thread causing an abort at asignate thread
body: <p>In my class I have thread object as public membe:</p>
  
  <pre><code>class AppManager{
     ...
      public:
      std::thread m_thread;
  };
  </code></pre>
  
  <p>Then when I initialize the thread:</p>
  
  <pre><code>void* BridgeFunction(void *pctx) {
      ((AppManager*)pctx)-&gt;MainAppThread();
      return 0;
  }
  
  void AppManager::CreateAppThread(){
     m_thread = std::thread(&amp;BridgeFunction, this);
     m_thread.detach();
  }
  </code></pre>
  
  <p>I got an Abort on std::terminate:</p>
  
  <pre><code> thread&amp; operator=(thread&amp;&amp; __t) noexcept
      {
        if (joinable())
        std::terminate();
        swap(__t);
        return *this;
      }
  </code></pre>
  
  <p>Why? I'm calling detach, I tried changing from:</p>
  
  <pre><code>m_thread = std::thread(&amp;BridgeFunction, this);
  </code></pre>
  
  <p>To:</p>
  
  <pre><code>m_thread = std::thread(&amp;AppManager::MainAppThread, this);
  </code></pre>
  
  <p>And the same, it works only if I declare the m_thread as global and I debugged why it works, turns out that the thread declared as member execute a random thread <strong>before</strong> I assign the thread, I know this because I printed the id to see if it was joinable:</p>
  
  <pre><code>    auto myid = m_thread.get_id();
      std::stringstream ss;
      ss &lt;&lt; myid;
      std::string thread_id = ss.str();
  
      LogPrintDebug("[Thread Activity] -  Id of thread not created yet: %s", thread_id.c_str());
  
      m_thread = std::thread(&amp;BridgeFunction, this);
      m_thread.detach();
  </code></pre>
  
  <p>It prints:</p>
  
  <pre><code>[Thread Activity] - Id of thread not created yet: 1074643408
  </code></pre>
  
  <p>When I declare global the thread it prints:</p>
  
  <pre><code>[Thread Activity] - Id of thread not created yet::id of a non-executing thread
  </code></pre>
  
  <p>I printed also the Id of my actual thread and it's different than the Id of the random thread, so try to terminate it due to the part:</p>
  
  <pre><code>if (joinable())
   std::terminate();
  </code></pre>
  
  <p>I'm using Visual Studio 2015 running an Android Native Activity using Clang 3.6 and the GNU STL libraries, ndk r10e.</p>
  
  <p>I tested the same code on windows and the id of the thread is 0, doesn't get any Abort.</p>
  
  <p>Thanks</p>