For two threads:
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?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;
}
}
public int read () {
int count = counts[0];
count = count + counts[1];
return count;
}
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;
}
public int read () {
int count = counts[0];
count = count + counts[1];
count = count + counts[2];
return count;
}
}
Is ThreeCounter
Linearizable?
ThreeCounter
Linearizable? public int read () {
int count = counts[0];
count = count + counts[1];
count = count + counts[2];
return count;
}
You will work through more problems on linearizability on Homework 3 and Quiz 3.
Nonblocking property $\implies$ existence of consistent response to pending method calls
Given a set of (concurrent) method calls a scheduler chooses sequence of methods that make steps
Conditions for implementations:
Is TwoCounter
lock-fee? Wait-free?
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;
}
}