An execution of a concurrent object is sequentially consistent if all method calls can be ordered such that:
An implementation of an object is sequentially consistent if
Queue supports enq(x)
and deq()
operations
enq
and deq
operations with lock
/unlock
lock
/unlock
Callsenq
/deq
are blocking
Can only change relative order of method calls if they overlap
A linearization point is a point in a method call where method “takes effect”
A concurrent execution is linearizable if:
An implementation of an object is linearizable if:
An incorrect (concurrent) counter
public class Counter {
int count = 0;
public void increment() { ++count; }
public int read() { return count; }
}
Better strategy (e.g., from lab 1)?
public class TwoCounter {
int[] counts = new int[2];
public void increment (int amt) {
int i = ThreadID.get(); // thread IDs are 0 and 1
int count = counts[i];
counts[i] = count + amt;
}
public int read () {
int count = counts[0];
count = count + counts[1];
return count;
}}
TwoCounter
Linearizable?increment
What is the linearization point of increment
?
public class TwoCounter {
public void increment (int amt) {
int i = ThreadID.get(); // thread IDs are 0 and 1
int count = counts[i];
counts[i] = count + amt;
}
read
What is the linearization point of read
?
public int read () {
int count = counts[0];
count = count + counts[1];
return count;
public int read () {
int count = counts[0];
count = count + counts[1];
return count;
The linearization point may depend on
How to generalize TwoCounter
to three threads?
public class ThreeCounter {
int[] counts = new int[3];
public void increment (int amt) {
int i = ThreadID.get(); // thread IDs are 0, 1, and 2
int count = counts[i];
counts[i] = count + amt;
}
}
read
Method public int read () {
int count = counts[0];
count = count + counts[1];
count = count + counts[2];
return count;
}
ThreeCounter
Linearizable? public int read () {
int count = counts[0];
count = count + counts[1];
count = count + counts[2];
return count;
}
Questions.
ThreeCounter
sequentially consistent?Linearizable Queues!