Content deleted Content added
No edit summary |
added alternative implementation |
||
Line 18:
The entry protocol uses normal memory reads to [[spin]] and wait for the lock to come 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:
'''procedure''' EnterCritical() {
'''while''' ( locked = true or TestAndSet(locked) = true )
'''skip''' ''// spin until locked''
}
==Caveat==
Although this [[optimization]] is usefull 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]].
==See also==
|