title: C semaphores and a "barrier" between threads
body: <p>I'm trying to solve a problem our Operating Systems professor showed us from a previous exam to prepare for the next one.</p>
  
  <p>The problem is to have two threads that execute concurrently and may complete in a different amount of time. After a particular thread is completed, it needs to block until the other thread is completed, then they may continue their execution.</p>
  
  <p>It seems conceptually simple to me, but my code isn't working the way I think it should.</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;semaphore.h&gt;
  
  #define N 10
  
  sem_t t_1_sem;
  sem_t t_2_sem;
  
  void *thread(void *vargp);
  /* shared by both threads*/
  struct {
      int count;
  } thread_count;
  
  int main() {
      pthread_t tid, tid1;
  
      thread_count.count = 0;
  
      sem_init(&amp;t_1_sem, 0, 1);
      sem_init(&amp;t_2_sem, 0, 1);
      printf("Hello from main thread! tid:%ld pid:%d\n", pthread_self(), getpid());
      pthread_create(&amp;tid, NULL, thread, NULL);
      pthread_create(&amp;tid1, NULL, thread, NULL);
  
      pthread_join(tid, NULL);
      pthread_join(tid1, NULL);
  
      exit(0);
  }
  
  void *thread(void *vargp) {
      int i, tid;
  
      int val, val2;
      sem_getvalue(&amp;t_1_sem, &amp;val);
      sem_getvalue(&amp;t_2_sem, &amp;val2);
      printf("initial value::: %d : %d\n", val, val2);
  
  
      tid = thread_count.count;
      thread_count.count += 1;
  
      for(i = 0;i&lt;N;i++){
          printf("%d, %d\n", tid, i);
          fflush(stdout);
          //sleep(0.1);
      }
  
      // TODO
      // barrier
      sem_getvalue(&amp;t_1_sem, &amp;val);
      sem_getvalue(&amp;t_2_sem, &amp;val2);
      printf("second value::: %d : %d\n", val, val2);
      int sem_val;
      if(tid == 0){
          // free other
          sem_getvalue(&amp;t_1_sem, &amp;sem_val);
          printf("posting to 2, waiting on 1 w/ %d count\n", sem_val);
          sem_post(&amp;t_2_sem);
          // wait on this one
          sem_wait(&amp;t_1_sem);
          printf("done waiting on 1\n");
      } else if(tid == 1){
          sem_getvalue(&amp;t_2_sem, &amp;sem_val);
          printf("posting to 1, waiting on 2 w/ %d count\n", sem_val);
          sem_post(&amp;t_1_sem);
          sem_wait(&amp;t_2_sem);
          printf("done waiting on 2\n");
      }
  
      sem_getvalue(&amp;t_1_sem, &amp;val);
      sem_getvalue(&amp;t_2_sem, &amp;val2);
      printf("final value::: %d : %d\n", val, val2);
      return NULL;
  }
  </code></pre>
  
  <p>What I'm expecting to see is both of the threads counting til 10, then the two "final value" <code>printf</code>'s happening next to each other. However, what I'm seeing is the "final value" prints occurring immediately after the thread finishes counting to 10 - it doesn't seem to wait.</p>
  
  <p>I'm also getting really weird values for the <code>sem_val</code> integer I print in the "posting to N" <code>printf</code>'s, e.g.:</p>
  
  <pre><code>Hello from main thread! tid:-1606277344 pid:5479
  initial value::: 0 : 0
  0, 0
  initial value::: 0 : 0
  1, 0
  0, 1
  1, 1
  0, 2
  1, 2
  1, 3
  1, 4
  1, 5
  0, 3
  1, 6
  0, 4
  1, 7
  0, 5
  1, 8
  0, 6
  1, 9
  0, 7
  second value::: 0 : 0
  posting to 1, waiting on 2 w/ -1809628646 count
  0, 8
  done waiting on 2
  final value::: 0 : 0
  0, 9
  second value::: 0 : 0
  posting to 2, waiting on 1 w/ -1809628646 count
  done waiting on 1
  final value::: 0 : 0
  </code></pre>
  
  <p>Any ideas/hints?</p>