title: Why do these threads not execute in the same order as the code?
body: <p>I'm new to threading in C and was messing around in an attempt to learn about it. I compiled and executed the (very basic) code below:</p>
  
  <pre><code>void *thread(void *vargp);
  
  int main(int argc, char **argv) {
    pthread_t tid1, tid2;
  
    printf("Hello from the main thread.\n");
  
    printf("Creating thread 1.\n");
    pthread_create(&amp;tid1, NULL, thread, NULL); 
    printf("Creating thread 2.\n");
    pthread_create(&amp;tid2, NULL, thread, NULL);
  
    printf("Main thread is going to wait on peer threads.\n");
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    printf("Peer threads have completed.\n");
  
    return EXIT_SUCCESS;
  }
  
  void *thread(void *vargp) {
    pthread_t tid = pthread_self();
    printf("Hello from thread %u.\n", (unsigned int)tid);
    return NULL;
  }
  </code></pre>
  
  <p>I expected the output to be...</p>
  
  <pre><code>Hello from the main thread.
  Creating thread 1.
  Hello from thread [number].
  Creating thread 2.
  Hello from thread [number].
  ...
  </code></pre>
  
  <p>But instead, it was:</p>
  
  <pre><code>Hello from the main thread.
  Creating thread 1.
  Creating thread 2.
  Main thread is going to wait on peer threads.
  Hello from thread 3067947840.
  Hello from thread 3076340544.
  ...
  </code></pre>
  
  <p>Why was the output in this order? Did the two threads wait until the join function to execute or did they just happen to take that long? Do threads need to be joined to the main thread in order to print output to the console?</p>
  
  <p>Thanks for explaining this to me!!</p>