0% found this document useful (0 votes)
27 views1 page

Is My Spin Lock Implementation Correct and Optimal?

This document discusses a beginSpinLock function that implements a spin lock mechanism to lock access to a shared resource. The function first checks if the lock is available and if not, it burns the thread's time slice by looping for a short period (1 or 2 milliseconds) before rechecking the lock. This spinning avoids the overhead of blocking the thread and releasing the CPU. The document warns that if not implemented carefully, a spin lock can cause livelock where the CPU is constantly busy spinning but no progress is made. It also provides a link to additional resources on implementing lightweight mutexes.

Uploaded by

waldo mher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

Is My Spin Lock Implementation Correct and Optimal?

This document discusses a beginSpinLock function that implements a spin lock mechanism to lock access to a shared resource. The function first checks if the lock is available and if not, it burns the thread's time slice by looping for a short period (1 or 2 milliseconds) before rechecking the lock. This spinning avoids the overhead of blocking the thread and releasing the CPU. The document warns that if not implemented carefully, a spin lock can cause livelock where the CPU is constantly busy spinning but no progress is made. It also provides a link to additional resources on implementing lightweight mutexes.

Uploaded by

waldo mher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

void beginSpinLock(lock)

{
if(lock) loopFor(1 milliseconds);
else
{
lock = true;
return;
}

if(lock) loopFor(2 milliseconds);


else
{
lock = true;
return;
}

// important is that the part above never


// cause the thread to sleep.
// It is "burning" the time slice of this thread.
// Hopefully for good.

// some implementations fallback to OS lock mechanism


// after a few tries
if(lock) return beginLock(lock);
else
{
lock = true;
return;
}
}
If your implementation is not careful, you can fall on livelock, spending all CPU on the lock
mechanism.

Also see:

https://preshing.com/20120226/roll-your-own-lightweight-mutex/
Is my spin lock implementation correct and optimal?

Summary:

Deadlock: situation where nobody progress, doing nothing (sleeping, waiting etc..). CPU
usage will be low;

Livelock: situation where nobody progress, but CPU is spent on the lock mechanism and
not on your calculation;

Starvation: situation where one procress never gets the chance to run; by pure bad luck or
by some of its property (low priority, for example);

Spinlock: technique of avoiding the cost waiting the lock to be freed.

You might also like