title: Getting Native Handle from Within a Thread?
body: <p>I am using VS2012 and I want to set thread priority from within a running thread. The goal is to initialize all threads with the highest priority state. To do this I want to get a <code>HANDLE</code> to the thread.</p>
  
  <p>I am having some trouble accessing the pointer that corresponds to the <code>thread</code> object.</p>
  
  <p>Is this possible?</p>
  
  <p>From the calling main thread, the pointer is valid and from the C++11 thread it is set to <code>CCCCCCCC</code>. Predictably dereferencing some nonsense memory location causes a crash. </p>
  
  <p>The code below is a simplified version showing the problem.</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;Windows.h&gt;
  #include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;condition_variable&gt;
  #include &lt;iostream&gt;
  #include &lt;atomic&gt;
  using namespace std;
  class threadContainer
      {
      thread* mT;
      condition_variable* con;
      void lockMe()
          {
          mutex m;
          unique_lock&lt;std::mutex&gt; lock(m);
          con-&gt;wait(lock);//waits for host thread
          cout &lt;&lt; mT &lt;&lt; endl;//CCCCCCCC
          auto h = mT-&gt;native_handle();//causes a crash
          con-&gt;wait(lock);//locks forever
          }
      public:
          void run()
              {
              con = new condition_variable();
              mT = new thread(&amp;threadContainer::lockMe,*this);
              cout &lt;&lt; mT &lt;&lt; endl; //00326420
              con-&gt;notify_one();// Without this line everything locks as expected
              mT-&gt;join();
              }
      };
  int _tmain(int argc, _TCHAR* argv[])
      {
      threadContainer mContainer;
      mContainer.run();
      return 0;
      }
  </code></pre>