title: wait for all threads finishing one job then doing another
body: <p>Suppose there are <code>NUM_TREADS</code> threads and they have to complete <code>job1()</code> before doing <code>job2()</code>. How to guarantee this to happen, like this:</p>
  
  <pre><code>void thread_func(void *arg)
  {
      while(1) {
          job1();
          some_kind_of_waiting();
          job2();
      }
      return NULL;
  }
  </code></pre>
  
  <p>Will a semaphore like following work or are there any other/better solutions?</p>
  
  <pre><code>{
      static int done;
      static sem_t semaphore;
  
      if(__sync_fetch_and_add(&amp;done, 1) == THREAD_NUMS-1) {
          done = 0;
          for(i = 0; i &lt; THREAD_NUMS-1; i++)
              sem_post(&amp;semaphore);
      } else
          sem_wait(&amp;semaphore);
  }
  </code></pre>
  
  <p>Thanks.</p>