Hello,
I think i have discovered what gone wrong with the following Spinlock
with a Dynamic Sleep:
http://code.google.com/p/gpdelphiunits/source/browse/trunk/src/SpinLock.pas?r=37
First you will notice that they are using a Dynamic Sleep , not
an exponential backoff, and that's not correct, cause you have to use an
exponential backoff to be able to lower correctly the contention,
second, if you have noticed this Dynamic Sleep or the exponential
backoff do use the Sleep() function to lower the contention , but
this is not correct, cause when you use an exponential backoff with
the Sleep() when the CAS fails under contention it will sleep for
milliseconds and that's too long and it will make you Spinlock very
slow, so the correct solution is to use the PAUSE instruction instead of
Sleep() like this:
for i:=0 to LSleepAmount do asm pause end;
and after that call also Sleep(0), to not freeze your computer.
I have patched the above Spinlock like that adding an exponential
backoff that uses
the pause instruction instead of the Sleep() and the Spinlock
have worked perfectly even under contention.
So i think i will return now back to the Spinlock with Exponential backoff
cause it has a good performance.
Thank you,
Amine Moulay Ramdane.