Lecture 11: And More Linearizability and Progress

Overview

  1. Counter, Revisited, Again
  2. Progress Condiditions

A Counter

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;
    }	
}

Is 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;
    }	
}

Linearization Point?

    public int read () {
        int count = counts[0];
        count = count + counts[1];
        return count;
    }	

More 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;		
    }
	
    public int read () {
        int count = counts[0];
        count = count + counts[1];
        count = count + counts[2];		
        return count;
    }	
}

Is ThreeCounter Linearizable?

Is ThreeCounter Linearizable?

    public int read () {
        int count = counts[0];
        count = count + counts[1];
        count = count + counts[2];		
        return count;
    }	

Conclusion

  • Linearizability is…
    • …a reasonable correctness condition
    • …nonblocking
    • …compositional
    • …subtle to reason about

You will work through more problems on linearizability on Homework 3 and Quiz 3.

Progress Conditions

Is Nonblocking Property Practical?

Nonblocking property $\implies$ existence of consistent response to pending method calls

  • Does not ensure that such a response can be found easily

Scheduler

Given a set of (concurrent) method calls a scheduler chooses sequence of methods that make steps

  • in computers, OS is typically scheduler
  • scheduler may be fair, may not be
  • want progress guarantees independent of scheduler

Nonblocking Progress Conditions

Conditions for implementations:

  • Wait-freedom: pending method invocation always completes in a finite number of steps
  • Lock-freedom: among all pending method calls, some method completes in a finite number of steps

Question

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;
    }
}