Content deleted Content added
m Corrected the comment in line 4, of the first pseudocode. |
Ethanpet113 (talk | contribs) →top: Explain the reason why we need test and test and set over test and set. |
||
Line 3:
To lower the overhead a more elaborate locking protocol '''test and test-and-set'''
is used. The main idea is to reduce [[writeback]] that can create [[resource contention]] when two seperate threads want the same lock. If ''n'' threads are competing for the lock, they will attempt to acquire it as soon as it is released if only using [[test and set]], causing each thread to invalidate the lock flag, meaning it must be propagated through the cache of the remaining processors ''n'' times, before any one thread may safely read it. By adding the ''check-yield'' step, only the first thread of execution to notice the lock is free will atempt to obtain it, eliminating the writeback.
''boolean'' locked := false ''// shared lock variable''
'''procedure''' EnterCritical() {
'''do''' {
'''while''' (locked == true) yield(); //
} '''while''' TestAndSet(locked) ''// actual atomic locking''
}
'''procedure''' TestAndSet(lock) {
boolean initial = lock;
lock = true;
return initial;
}
Exit protocol is:
|