title: Start thread within member function using std::thread & std::bind
body: <p>I have few queries with respect to below code snapshot.</p>
  
  <p>1) With respect to pthread_create(), assume Thread_1 creates Thread_2. To my understanding Thread_1 can exit without join, but still Thread_2 will keep running. Where as in below example without join() I am not able to run thread and I am seeing exceptions.</p>
  
  <p>2) In few examples I am seeing thread creation without thread object as below. But when I do the same, code is terminated.</p>
  
  <pre><code>std::thread(&amp;Task::executeThread, this);
  
  I am compiling with below command.
  g++ filename.cpp -std=c++11 -lpthread
  </code></pre>
  
  <p>But still it terminate with exception. Is this right way of creating thread or is there any different version of C++ (In my project already they are compiling but not sure about the version).</p>
  
  <p>3) In few examples of my project code, I am seeing below way of creating thread. But I am not able to execute with below example.</p>
  
  <pre><code>std::thread( std::bind(&amp;Task::executeThread, this) );
  </code></pre>
  
  <p>Below is my code snapshot.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  
  class Task
  {
      public:
      void executeThread(void)
      {
          for(int i = 0; i &lt; 5; i++)
          {
             std::cout &lt;&lt; " :: " &lt;&lt; i &lt;&lt; std::endl;
          }
      }
  
      void startThread(void);
  };
  
  void Task::startThread(void)
  {
      std::cout &lt;&lt; "\nthis: " &lt;&lt; this &lt;&lt; std::endl;
  
  #if 1
      std::thread th(&amp;Task::executeThread, this);
      th.join(); // Without this join() or while(1) loop, thread will terminate
      //while(1);
  #elif 0
      std::thread(&amp;Task::executeThread, this);  // Thread creation without thread object
  #else
      std::thread( std::bind(&amp;Task::executeThread, this) );
      while(1);
  #endif
  }
  
  int main()
  {
      Task* taskPtr = new Task();
      std::cout &lt;&lt; "\ntaskPtr: " &lt;&lt; taskPtr &lt;&lt; std::endl;
  
      taskPtr-&gt;startThread();
  
      delete taskPtr;
      return 0;
  }
  </code></pre>
  
  <p>Thanks &amp; Regards</p>
  
  <p>Vishnu Beema</p>