title: the execute order of threads
body: <p>The result of following code is:</p>
  
  <pre><code>main() : creating thread, 0
  main() : creating thread, 1
  main() : creating thread, 2
  main() : creating thread, 3
  main() : creating thread, 4
  Hello World! Thread ID, 0
  Hello World! Thread ID, 1
  Hello World! Thread ID, 2
  Hello World! Thread ID, 3
  Hello World! Thread ID, 4
  </code></pre>
  
  <p>I don't know why the thread is execute after all the <code>main()</code> finished, for example, after <code>cout &lt;&lt; "main() : creating thread, " &lt;&lt; i &lt;&lt; endl;</code> a new thread created, so why the result is not </p>
  
  <pre><code>main() : creating thread, 0
  Hello World! Thread ID, 0
  .......
  
  
  
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;pthread.h&gt;
  
  using namespace std;
  
  #define NUM_THREADS     5
  
  void *PrintHello(void *threadid)
  {
     long tid;
     tid = (long)threadid;
     cout &lt;&lt; "Hello World! Thread ID, " &lt;&lt; tid &lt;&lt; endl;
     pthread_exit(NULL);
  }
  
  int main ()
  {
     pthread_t threads[NUM_THREADS];
     int rc;
     int i;
     for( i=0; i &lt; NUM_THREADS; i++ ){
        cout &lt;&lt; "main() : creating thread, " &lt;&lt; i &lt;&lt; endl;
        rc = pthread_create(&amp;threads[i], NULL, 
                            PrintHello, (void *)i);
        if (rc){
           cout &lt;&lt; "Error:unable to create thread," &lt;&lt; rc &lt;&lt; endl;
           exit(-1);
        }
     }
     pthread_exit(NULL);
  }
  </code></pre>