title: pthread_join() for asynchronous threads
body: <p>I have written a simple demonstration program so that I can understand the <code>pthread_join()</code> function.</p>
  
  <p>I know how to use the <code>pthread_condition_wait()</code> function to allow asynchronous threading but I'm trying to understand how I can do similar work using the <code>pthread_join()</code> function.</p>
  
  <p>In the program below I pass <em>Thread 1s</em> ID to <em>Thread 2s</em> function. Inside <em>Thread 2s</em> function I call the <code>pthread_join()</code> function and pass in <em>Thread 1s</em> ID. I expected this to cause <em>Thread 1</em> to run first and then for <em>Thread 2</em> to run second, but what I'm getting is that they both run concurrently.</p>
  
  <p>Is this because only one thread can use the <code>pthread_join()</code> function at a time and that I am already using the <code>pthread_join()</code> function when I call it from the <em>main thread</em>?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  
  void *functionCount1();
  void *functionCount2(void*);
  
  int main()
  {
  
      /* 
          How to Compile
           gcc -c foo
           gcc -pthread -o foo foo.o
      */
  
      printf("\n\n");
  
      int rc;
      pthread_t thread1, thread2;
  
      /* Create two thread --I took out error checking for clarity*/
      pthread_create( &amp;thread1, NULL, &amp;functionCount1, NULL)
      pthread_create( &amp;thread2, NULL, &amp;functionCount2, &amp;thread1)
  
      pthread_join( thread1, NULL);
      pthread_join( thread2, NULL);
  
      printf("\n\n");
      exit(0);
  }
  
  void *functionCount1()
  {
  
      printf("\nFunction 1");
          sleep(5);
      printf("\nFunction 1");
  
      return(NULL);
  }
  
  void *functionCount2(void* argument)
  {
  
      pthread_t* threadID = (pthread_t*) argument;
  
      pthread_join(*threadID, NULL);
  
      printf("\nFunction 2");
          sleep(5);
      printf("\nFunction 2");
  
      return(NULL);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <p><img src="https://i.stack.imgur.com/ELDv1.png" alt="enter image description here"></p>