title: How to use sem_trywait()?
body: <p>How can I test whether a semaphore blocked?</p>
  
  <p>i try function <code>sem_trywait()</code> but it not wokring, i dont now why, can you help me please?</p>
  
  <p>Example is here(Example program that uses semaphores to solve sleeping barber problem.): <a href="http://users.dickinson.edu/~braught/courses/cs354s00/classes/code/SleepBarber.src.html" rel="nofollow">http://users.dickinson.edu/~braught/courses/cs354s00/classes/code/SleepBarber.src.html</a></p>
  
  <p>I want use <code>sem_trywait()</code> to detect, is semaphore blocket:</p>
  
  <pre><code> void *customer(void *number) {
      int num = *(int *)number;
  
      //there is my problem...
      //you must waiting to free semaphore...
      while(sem_trywait(&amp;waitingRoom)){
          printf("Semaphore is full you must wait!");
      }
  
      // Wait for space to open up in the waiting room...
      sem_wait(&amp;waitingRoom);
      printf("Customer %d entering waiting room.\n", num);
  
      // Wait for the barber chair to become free.
      sem_wait(&amp;barberChair);
  
      // The chair is free so give up your spot in the
      // waiting room.
      sem_post(&amp;waitingRoom);
  
      // Wake up the barber...
      printf("Customer %d waking the barber.\n", num);
      sem_post(&amp;barberPillow);
  
      // Wait for the barber to finish cutting your hair.
      sem_wait(&amp;seatBelt);
  
      // Give up the chair.
      sem_post(&amp;barberChair);
      printf("Customer %d leaving barber shop.\n", num);
  }
  </code></pre>
  
  <p>The proble is in while loop</p>
  
  <pre><code>while(sem_trywait(&amp;waitingRoom)){
      printf("Semaphore is full you must wait!");
  }
  </code></pre>
  
  <p>I dont know how to use it. Thank you.</p>
  
  <hr>
  
  <p><strong>EDIT1:</strong></p>
  
  <pre><code>void *customer(void *number) {
      int num = *(int *) number;
  
      // Leave for the shop and take some random amount of
      // time to arrive.
  
      printf("Customer %d arrived at barber shop.\n", num);
  
      if (sem_trywait(&amp;waitingRoom) == 0) {
          // Wait for space to open up in the waiting room...
          sem_wait(&amp;waitingRoom);
          printf("Customer %d entering waiting room--------------------------------------------.\n", num);
  
          // Wait for the barber chair to become free.
          sem_wait(&amp;barberChair);
  
          // The chair is free so give up your spot in the
          // waiting room.
          sem_post(&amp;waitingRoom);
  
          // Wake up the barber...
          printf("Customer %d waking the barber.\n", num);
          sem_post(&amp;barberPillow);
  
          // Wait for the barber to finish cutting your hair.
          sem_wait(&amp;seatBelt);
  
          // Give up the chair.
          sem_post(&amp;barberChair);
          printf("Customer %d leaving barber shop.\n", num);
      } else {
          printf("leaving barber shop %d\n", num);
          customer(&amp;num);
          //sem_wait(X);
      }
  
  
  }
  </code></pre>