title: C: Threading semaphore_wait vs while loop
body: <p>Is there any difference between the following pieces of code in terms of processor usage.</p>
  
  <pre><code>void *ManageSequencer(void *argument){
    SomeClass *someClass = (SomeClass *)argument;
  
    while (someClass-&gt;ThreadIsAlive()) {
  
      while(someClass-&gt;isAsleep) { }
  
      someClass-&gt;isAsleep = true;
  
      //thread execution statements 
  
    }
    return argument;
  }
  </code></pre>
  
  <p>where some class sets <code>isAsleep=false</code> periodically when it needs the thread to execute</p>
  
  <p>OR</p>
  
  <pre><code>void *ManageSequencer(void *argument){
    SomeClass *someClass = (SomeClass *)argument;
  
    while (someClass-&gt;ThreadIsAlive()) {
  
      semaphore_wait(sem);
  
      //thread execution statements 
  
    }
    return argument;
  }
  </code></pre>
  
  <p>where someClass calls <code>semaphore_signal(sem);</code> periodically when it needs the thread to execute.</p>
  
  <p>this question is not about atomicity, just whether the while loop will cause the processor more work than the semaphore solution. Does the semaphore just have a while loop inside it that blocks until a condition is met (increment of semaphore above zero)?</p>