waspswarm
waspswarm

Reputation: 13

Help with OSSpinLock* usage to replace a while(true) {sleep(1);}

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);

Upvotes: 0

Views: 1817

Answers (1)

Adam Rosenfield
Adam Rosenfield

Reputation: 400274

Don't use a spinlock -- spinlocks waste CPU cycles, looping endlessly until they get the lock. Spinlocks should only be used when the time that the lock will be held will be very short.

A better alternative would be to use a condition variable, such as the ones available in the pthreads library. This way, your thread will sleep and not waste any CPU cycles until the condition is satisfied. See the example listed in that link for how to use pthreads condition variables.

Upvotes: 5

Related Questions