Content deleted Content added
→Caveat: Be more specific about why double-checked locking is an anti-pattern and link to solution. |
https://en.wikipedia.org/w/index.php?title=Test_and_test-and-set&oldid=869387971 seems to have misunderstood that the pseudocode in https://en.wikipedia.org/wiki/Test-and-set#Software_implementation_of_test-and-set is only pseudocode and is _missing_ "actual atomic locking". The yield() doesn't help. As such, i think previous versions of the text of this section are clearer. Really, this article should maybe just be a section in the test-and-set article. |
||
Line 2:
[[mutual exclusion]] in [[multiprocessor]] environments. Although a correct [[lock (computer science)|lock]] can be implemented with test-and-set, it can lead to [[resource contention]] in busy lock (caused by bus locking and cache invalidation when test-and-set operation needs to access memory [[atomic (computer science)|atomically]]).
To lower the overhead a more elaborate locking protocol '''test and test-and-set''' is used.
Given a lock:
''boolean'' locked := false ''// shared lock variable''
Entry protocol is:
'''procedure''' EnterCritical() {
'''do''' {
'''while''' ( locked == true
} '''while''' ( ''TestAndSet''(locked) == true ) ''// attempt actual atomic locking using the [[test-and-set]] instruction''
}
Exit protocol is:
Line 21 ⟶ 19:
}
The entry protocol uses normal memory reads to
If the [[programming language]] used supports [[short-circuit evaluation]], the entry protocol could be implemented as:
|