Test and test-and-set: Difference between revisions

Content deleted Content added
Jyke (talk | contribs)
added alternative implementation
Jyke (talk | contribs)
m spelling
Line 3:
 
To lower the overhead a more elaborate locking protocol ''Test and Test-and-set''
is used. The main idea is '''not''' to spin in [[test-and-set]] but increase the likelihood of successfullsuccessful [[test-and-set]] by using following entry protocol to the lock:
 
''boolean'' locked := false ''// shared lock variable''
Line 17:
}
 
The entry protocol uses normal memory reads to [[spin]] and wait for the lock to comebecome free. [[Test-and-set]] is only used to try to get the lock when normal memory read says its free. Thus the expensive atomic memory operations happens less often than in simple spin around [[Test-and-set]].
 
If the [[programming language]] used supports [[Lazy evaluation#Minimal evaluation|minimal evaluation]], the entry protocol could be implemented as:
Line 28:
 
==Caveat==
Although this [[optimization]] is usefulluseful in [[system programming]] it should be avoided in high level [[concurrent programming]]. One example of bad usage of this [[idiom]] is [[double-checked locking]], which is listed as an [[anti-pattern]].