Queue locks,( Array based locks, CLH, MCS Queue locks, composite locks) Flashcards
Why are queue locks good?
Cache-coherence traffic is reduced since each thread spins on a different location.
increase CS utilization
fair
variables in Alock class
class ALock implements Lock {
boolean[] flags={true,false,…,false};
AtomicInteger tail
= new AtomicInteger(0);
ThreadLocal<Integer> mySlot;</Integer>
Lock code for Alock
public lock() {
mySlot = tail.getAndIncrement();
while (!flags[mySlot]) {};
}
unlock for Alock
public unlock() {
flags[mySlot] = false;
flags[(mySlot+1)] = true;
}
What is good/bad about Alock
scalable,simple
not space efficient
1bit per thread
What keeps track of the queue in CLH?
Linked List
What are the 2 states of the CLH and what do they mean
true: has/want to acquire lock
false: finished + has released it
What does each node have in a CLH
a track of its predecessors status
Variables in CLH
class CLHLock implements Lock {
AtomicReference<Qnode> tail;
ThreadLocal<Qnode> myNode
= new Qnode();
}}</Qnode></Qnode>
lock CLH
public void lock() {
myNode.locked = true;
Qnode pred
= tail.getAndSet(myNode);
while (pred.locked) {}
}
unlock CLH
public void unlock() {
myNode.locked.set(false);
myNode = pred;
}
Whats good about CLH
Lock release affects predecessor only
small, constant-sized space
MCS Lock idea
FIFO
Spin on local memory only
small, constant-size overhead
Difference between CLH and MCS
in CLH = implicit MCS = explicit
each node in the queue has a next field
how does the Node look for MCS
class Qnode {
boolean locked = false;
qnode next = null;
}