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,
while ( mState != RELEASED )
{
sleep( 1 );
}
There is a post render maintenance callback that is fed to AudioUnitAddRenderNotify() that would set the mState of these various objects.
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).
Would this be a reasonable equivalent? Should I also add some lock protection in the maintenance thread which manipulates the mState of these objects?
OSSpinLock spin_lock = OS_SPINLOCK_INIT;
if (!OSSpinLockTry(&spin_lock))
OSSpinLockLock(&spin_lock);
while (mState != RELEASED)
OSSpinLockTry(&spin_lock);
OSSpinLockUnlock(&spin_lock);