title: Sem_wait() not blocking after first true condition
body: <p>I am learning to use semaphores and below is a small scenario which I've tried to implement. Its behaving weird in a way. After sem_wait() gets unblocked first time, its not getting blocked again and keeps on looping, not getting why. Is this the right way or right scenario to use semaphore?</p>
  
  <p>EDIT: I just realized that if I uncomment the sleep after sem_post, it works fine. .Reason being it was repeatedly doing sem_post() before thread could do coin=0 I believe. But is it right to use sleep this way with semaphores. I believe this would be considered a bad practice?</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;string.h&gt;
  #include &lt;semaphore.h&gt;
  
  
  #define MAX_MSG_LEN  256
  
  sem_t sem1;
  sem_t sem2;
  int coin=0;
  
  void *thrdFun1(void *arg);
  void *thrdFun2(void *arg);
  void toggleCase(char *buf, int cnt);
  
  int main()
  {
      pthread_t thrd1;
      char argmsg1[] = "Thread1: Waiting to deliver\n";
      int thNum;
      int res;
  
      res = sem_init(&amp;sem1, 0,0);     
  //  res = sem_init(&amp;sem2, 0,0);     
  
      res = pthread_create(&amp;thrd1, NULL, thrdFun1, argmsg1);      
  
      while(1)
      {
      if (coin==0)
          {
          printf("no coin: please enter coin\n"); 
          scanf("%d",&amp;coin);
          }
      else
          {
          sem_post(&amp;sem1);
  //      sleep(1);
          }
      }
  
      return 0;
  }
  
  void *thrdFun1(void *arg)
  {
      while(1)
      {
          printf("I'm %s\n",(char *)arg);
  
      sem_wait(&amp;sem1);
  
      printf("Delivered...\n");
      coin=0;
      sleep(1);
      }
  }       
  </code></pre>