title: Pthread Usage in C++
body: <p>I am currently making intern and I am asked to write a multi client server-client application with using C++. Hence, I'm trying to learn threading. Have one question:</p>
  
  <p>I want to print "you are in thread A", then "you are in thread B", "now you are again in thread A". However it only prints first two sentences and ignores endl command. Can't exactly understand how it works. How to fix that and could you briefly explain working mechanism? </p>
  
  <p>Why main thread exits before all function calls completed?</p>
  
  <pre><code>void  * function1(void * arg);
  void  * function2(void * arg);
  
  
  pthread_t thr_A, thr_B;
  int main( void )
  {
  
      pthread_create(&amp;thr_A, NULL, function1, (void*)thr_B); 
      pthread_create(&amp;thr_B, NULL, function2,NULL); 
  
  return 0;
  
  }
  
  void * function1(void * arg)
  {
  
    cout &lt;&lt; "You are in thread A" &lt;&lt; endl;
    pthread_join(thr_B, NULL);
    cout &lt;&lt; "now you are again in thread A" &lt;&lt; endl; 
    pthread_exit((void*)thr_A);
  
  
  }
  
  void * function2(void * arg)
  {
      cout &lt;&lt; " you are in thread B "  &lt;&lt; endl ;
      pthread_exit((void*)thr_B);
  }
  </code></pre>