Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #1747

About Spinlock...

From aminer <aminer@toto.net>
Newsgroups comp.programming.threads, comp.programming
Subject About Spinlock...
Date 2013-09-24 18:27 -0700
Organization albasani.net
Message-ID <l1t3le$tdh$1@news.albasani.net> (permalink)

Cross-posted to 2 groups.

Show all headers | View raw


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.






Back to comp.programming.threads | Previous | NextNext in thread | Find similar


Thread

About Spinlock... aminer <aminer@toto.net> - 2013-09-24 18:27 -0700
  Re: About Spinlock... aminer <aminer@toto.net> - 2013-09-24 18:58 -0700
  Re: About Spinlock... aminer <aminer@toto.net> - 2013-09-24 19:00 -0700

csiph-web