title: Why semaphore object is not initialized?
body: <p>I'm learning to use semaphore object. But I can't initialize it.
  A sem_init function always return value -1 rain or shine.</p>
  
  <p>return value -1 indicates first argument is not valid pointer, say my reference.
  But I can't find miss print in my code. I compiled my code in Xcode on OS X.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;semaphore.h&gt;
  
  void * thread_snd(void *arg);
  void * thread_rcv(void* arg);
  
  sem_t bin_sem;
  int number = 0;
  
  char thread1[] = "A thread";
  char thread2[] = "B thread";
  char thread3[] = "C thread";
  
  int main(int argc, char** argv)
  {
      pthread_t t1, t2 ,t3;
      void *thread_result;
      int state;
  
      state       =   sem_init(&amp;bin_sem, 0, 0);
      if(state != 0)
      {
          puts("fail to initialize semaphore");
          exit(1);
      }
  
      pthread_create(&amp;t1, NULL, thread_snd, &amp;thread1);
      pthread_create(&amp;t2, NULL, thread_rcv, &amp;thread2);
      pthread_create(&amp;t3, NULL, thread_rcv, &amp;thread3);
  
      pthread_join(t1, &amp;thread_result);
      pthread_join(t2, &amp;thread_result);
      pthread_join(t3, &amp;thread_result);
  
      printf("final number : %d \n", number);
      sem_destroy(&amp;bin_sem);
      return 0;
  }
  
  void * thread_snd(void * arg)
  {
      int i;
      for(i = 0 ; i &lt; 4; i++)
      {
          while(number != 0)
              sleep(1);
          number++;
          printf("execution : %s, number : %d \n", (char*) arg, number);
          sem_post(&amp;bin_sem);
      }
  }
  
  void * thread_rcv(void* arg)
  {
      int i;
      for(i = 0 ; i &lt; 2; i++)
      {
          sem_wait(&amp;bin_sem);
          number--;
          printf("execution : %s number : %d \n", (char*)arg, number);
      }
  }
  </code></pre>