Lecture 10: More Linearizability and Progress

Overview

  1. Counter, Revisited
  2. Progress Condiditions

Note on Pixels and Points

Counter, Revisited

Recall

A concurrent execution is linearizable if:

  • exists a linearization point in each method call such that execution is consistent with sequential execution where method calls occur in order of corresponding linearization points

An implementation of an object is linearizable if:

  • it guarantees every execution is linearizable

A Bad Counter

public class Counter {
    int count = 0;
    
    public void increment () {
        ++count;
    }
    
    public int read () {
        return count;
    }
}

Why is this bad with multiple threads, even if read/write operations are atomic?

A Better 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 this better?

Download the Worksheet

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

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?

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.