title: Help with OSSpinLock* usage to replace a while(true) {sleep(1);}
body: <p>I am maintaining a carbon C++ application on OS X. There is a "spin lock" in one of the objects in the code that looks like this,</p>
  
  <pre><code>while ( mState != RELEASED )
  {
      sleep( 1 );
  }
  </code></pre>
  
  <p>There is a post render maintenance callback that is fed to AudioUnitAddRenderNotify() that would set the mState of these various objects.</p>
  
  <p>I was thinking about using the built-in OSSpinLock family of functions to replace this (if nothing else because they are thread safe and the above code doesn't seem to be).</p>
  
  <p>Would this be a reasonable equivalent? Should I also add some lock protection in the maintenance thread which manipulates the mState of these objects?</p>
  
  <pre><code>OSSpinLock spin_lock = OS_SPINLOCK_INIT;
  
  if (!OSSpinLockTry(&amp;spin_lock))
      OSSpinLockLock(&amp;spin_lock);
  
  while (mState != RELEASED)
      OSSpinLockTry(&amp;spin_lock);
  
  OSSpinLockUnlock(&amp;spin_lock);
  </code></pre>