Concurrency - 6.037 - Structure and Interpretation of...

118
Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike Phillips <mpp> Massachusetts Institute of Technology Lecture 8A Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 1 / 27

Transcript of Concurrency - 6.037 - Structure and Interpretation of...

Page 1: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency6.037 - Structure and Interpretation of Computer Programs

Mike Phillips <mpp>

Massachusetts Institute of Technology

Lecture 8A

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 1 / 27

Page 2: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Processor speed

http://en.wikipedia.org/wiki/File:Transistor_Count_and_Moore%27s_Law_-_2011.svg

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 2 / 27

Page 3: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Processor speed

Year

MIP

S/C

PU

clo

ck s

peed

100

101

102

103

1980 1985 1990 1995 2000 2005 2010

http://csgillespie.wordpress.com/2011/01/25/cpu-and-gpu-trends-over-time/ with data originally from Wikipedia

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 3 / 27

Page 4: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple processors

Nowadays every laptop has multiple “cores” in it

Fastest “supercomputers” all have thousands of processorsNot a new problem – Connection Machine (Lisp!) had 65,000processors (1980s)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 4 / 27

Page 5: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple processors

Nowadays every laptop has multiple “cores” in itFastest “supercomputers” all have thousands of processors

Not a new problem – Connection Machine (Lisp!) had 65,000processors (1980s)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 4 / 27

Page 6: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple processors

Nowadays every laptop has multiple “cores” in itFastest “supercomputers” all have thousands of processorsNot a new problem – Connection Machine (Lisp!) had 65,000processors (1980s)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 4 / 27

Page 7: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency

All of our code only makes use of one processor

Concurrency is the ability to do more than one computation inparallelIs a lot easier on the computer than on the programmer!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 5 / 27

Page 8: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency

All of our code only makes use of one processorConcurrency is the ability to do more than one computation inparallel

Is a lot easier on the computer than on the programmer!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 5 / 27

Page 9: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency

All of our code only makes use of one processorConcurrency is the ability to do more than one computation inparallelIs a lot easier on the computer than on the programmer!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 5 / 27

Page 10: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Objects with state

In purely functional programming, time of evaluation is irrelevant:

(define (add-17 x) (+ x 17))(add-17 10); => 27; ...later:(add-17 10); => 27

Just run sequences on different processors and we’re done!...except, this does not work for objects with state:(define alexmv

(new autonomous-person ’alexmv ’great-court 3 3))((alexmv ’LOCATION) ’NAME); => great-court; ...later:((alexmv ’LOCATION) ’NAME); => great-dome

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 6 / 27

Page 11: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Objects with state

In purely functional programming, time of evaluation is irrelevant:(define (add-17 x) (+ x 17))(add-17 10); => 27

; ...later:(add-17 10); => 27

Just run sequences on different processors and we’re done!...except, this does not work for objects with state:(define alexmv

(new autonomous-person ’alexmv ’great-court 3 3))((alexmv ’LOCATION) ’NAME); => great-court; ...later:((alexmv ’LOCATION) ’NAME); => great-dome

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 6 / 27

Page 12: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Objects with state

In purely functional programming, time of evaluation is irrelevant:(define (add-17 x) (+ x 17))(add-17 10); => 27; ...later:(add-17 10)

; => 27

Just run sequences on different processors and we’re done!...except, this does not work for objects with state:(define alexmv

(new autonomous-person ’alexmv ’great-court 3 3))((alexmv ’LOCATION) ’NAME); => great-court; ...later:((alexmv ’LOCATION) ’NAME); => great-dome

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 6 / 27

Page 13: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Objects with state

In purely functional programming, time of evaluation is irrelevant:(define (add-17 x) (+ x 17))(add-17 10); => 27; ...later:(add-17 10); => 27

Just run sequences on different processors and we’re done!...except, this does not work for objects with state:(define alexmv

(new autonomous-person ’alexmv ’great-court 3 3))((alexmv ’LOCATION) ’NAME); => great-court; ...later:((alexmv ’LOCATION) ’NAME); => great-dome

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 6 / 27

Page 14: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Objects with state

In purely functional programming, time of evaluation is irrelevant:(define (add-17 x) (+ x 17))(add-17 10); => 27; ...later:(add-17 10); => 27

Just run sequences on different processors and we’re done!

...except, this does not work for objects with state:(define alexmv

(new autonomous-person ’alexmv ’great-court 3 3))((alexmv ’LOCATION) ’NAME); => great-court; ...later:((alexmv ’LOCATION) ’NAME); => great-dome

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 6 / 27

Page 15: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Objects with state

In purely functional programming, time of evaluation is irrelevant:(define (add-17 x) (+ x 17))(add-17 10); => 27; ...later:(add-17 10); => 27

Just run sequences on different processors and we’re done!...except, this does not work for objects with state:

(define alexmv(new autonomous-person ’alexmv ’great-court 3 3))

((alexmv ’LOCATION) ’NAME); => great-court; ...later:((alexmv ’LOCATION) ’NAME); => great-dome

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 6 / 27

Page 16: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Objects with state

In purely functional programming, time of evaluation is irrelevant:(define (add-17 x) (+ x 17))(add-17 10); => 27; ...later:(add-17 10); => 27

Just run sequences on different processors and we’re done!...except, this does not work for objects with state:(define alexmv(new autonomous-person ’alexmv ’great-court 3 3))

((alexmv ’LOCATION) ’NAME); => great-court

; ...later:((alexmv ’LOCATION) ’NAME); => great-dome

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 6 / 27

Page 17: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Objects with state

In purely functional programming, time of evaluation is irrelevant:(define (add-17 x) (+ x 17))(add-17 10); => 27; ...later:(add-17 10); => 27

Just run sequences on different processors and we’re done!...except, this does not work for objects with state:(define alexmv(new autonomous-person ’alexmv ’great-court 3 3))

((alexmv ’LOCATION) ’NAME); => great-court; ...later:((alexmv ’LOCATION) ’NAME)

; => great-dome

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 6 / 27

Page 18: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Objects with state

In purely functional programming, time of evaluation is irrelevant:(define (add-17 x) (+ x 17))(add-17 10); => 27; ...later:(add-17 10); => 27

Just run sequences on different processors and we’re done!...except, this does not work for objects with state:(define alexmv(new autonomous-person ’alexmv ’great-court 3 3))

((alexmv ’LOCATION) ’NAME); => great-court; ...later:((alexmv ’LOCATION) ’NAME); => great-dome

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 6 / 27

Page 19: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency and time

Behavior of objects with state depends on sequence of events inreal time

This is fine in a concurrent program where that state is not sharedexplicitly or implicitly between threadsFor example, autonomous objects in our adventure gameconceptually act concurrently. We’d like to take advantage of thisby letting them run at the same timeBut how does the order of execution affect the interactionsbetween them?

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 7 / 27

Page 20: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency and time

Behavior of objects with state depends on sequence of events inreal timeThis is fine in a concurrent program where that state is not sharedexplicitly or implicitly between threads

For example, autonomous objects in our adventure gameconceptually act concurrently. We’d like to take advantage of thisby letting them run at the same timeBut how does the order of execution affect the interactionsbetween them?

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 7 / 27

Page 21: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency and time

Behavior of objects with state depends on sequence of events inreal timeThis is fine in a concurrent program where that state is not sharedexplicitly or implicitly between threadsFor example, autonomous objects in our adventure gameconceptually act concurrently. We’d like to take advantage of thisby letting them run at the same time

But how does the order of execution affect the interactionsbetween them?

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 7 / 27

Page 22: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency and time

Behavior of objects with state depends on sequence of events inreal timeThis is fine in a concurrent program where that state is not sharedexplicitly or implicitly between threadsFor example, autonomous objects in our adventure gameconceptually act concurrently. We’d like to take advantage of thisby letting them run at the same timeBut how does the order of execution affect the interactionsbetween them?

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 7 / 27

Page 23: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Canonical bank example

(define (make-account balance)(define (withdraw amount)

(if (>= balance amount)(begin (set! balance (- balance amount))

balance)"Insufficient funds"))

(define (deposit amount)(set! balance (+ balance amount)))

(define (dispatch msg)(cond ((eq? msg ’withdraw) withdraw)

((eq? msg ’deposit) deposit)((eq? msg ’balance) balance)(else (error "unknown request" msg))))

dispatch)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 8 / 27

Page 24: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Why is time an issue?

(define alex (make-account 100))(define ben alex)

((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

-10 9065 -25

Alex Bank Ben10075 -25

-10 65

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 9 / 27

Page 25: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Why is time an issue?

(define alex (make-account 100))(define ben alex)

((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

-10 9065 -25

Alex Bank Ben10075 -25

-10 65

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 9 / 27

Page 26: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Why is time an issue?

(define alex (make-account 100))(define ben alex)

((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

-10 90

65 -25

Alex Bank Ben10075 -25

-10 65

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 9 / 27

Page 27: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Why is time an issue?

(define alex (make-account 100))(define ben alex)

((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

-10 9065 -25

Alex Bank Ben10075 -25

-10 65

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 9 / 27

Page 28: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Why is time an issue?

(define alex (make-account 100))(define ben alex)

((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

-10 9065 -25

Alex Bank Ben100

75 -25-10 65

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 9 / 27

Page 29: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Why is time an issue?

(define alex (make-account 100))(define ben alex)

((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

-10 9065 -25

Alex Bank Ben100

75 -25

-10 65

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 9 / 27

Page 30: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Why is time an issue?

(define alex (make-account 100))(define ben alex)

((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

-10 9065 -25

Alex Bank Ben100

75 -25-10 65

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 9 / 27

Page 31: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Race conditions

(define (withdraw amount)(if (>= balance amount)

(begin (set! balance (- balance amount))balance)

"Insufficient funds"))((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

Access balance (100) 100100 Access balance (100)

New value 100 - 10 = 90 100100 New value 100 - 25 = 75

Set balance 90 9075 Set balance 7575

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 10 / 27

Page 32: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Race conditions

(define (withdraw amount)(if (>= balance amount)

(begin (set! balance (- balance amount))balance)

"Insufficient funds"))((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

Access balance (100) 100100 Access balance (100)

New value 100 - 10 = 90 100100 New value 100 - 25 = 75

Set balance 90 9075 Set balance 7575

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 10 / 27

Page 33: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Race conditions

(define (withdraw amount)(if (>= balance amount)

(begin (set! balance (- balance amount))balance)

"Insufficient funds"))((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

Access balance (100) 100

100 Access balance (100)New value 100 - 10 = 90 100

100 New value 100 - 25 = 75Set balance 90 90

75 Set balance 7575

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 10 / 27

Page 34: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Race conditions

(define (withdraw amount)(if (>= balance amount)

(begin (set! balance (- balance amount))balance)

"Insufficient funds"))((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

Access balance (100) 100100 Access balance (100)

New value 100 - 10 = 90 100100 New value 100 - 25 = 75

Set balance 90 9075 Set balance 7575

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 10 / 27

Page 35: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Race conditions

(define (withdraw amount)(if (>= balance amount)

(begin (set! balance (- balance amount))balance)

"Insufficient funds"))((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

Access balance (100) 100100 Access balance (100)

New value 100 - 10 = 90 100

100 New value 100 - 25 = 75Set balance 90 90

75 Set balance 7575

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 10 / 27

Page 36: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Race conditions

(define (withdraw amount)(if (>= balance amount)

(begin (set! balance (- balance amount))balance)

"Insufficient funds"))((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

Access balance (100) 100100 Access balance (100)

New value 100 - 10 = 90 100100 New value 100 - 25 = 75

Set balance 90 9075 Set balance 7575

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 10 / 27

Page 37: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Race conditions

(define (withdraw amount)(if (>= balance amount)

(begin (set! balance (- balance amount))balance)

"Insufficient funds"))((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

Access balance (100) 100100 Access balance (100)

New value 100 - 10 = 90 100100 New value 100 - 25 = 75

Set balance 90 90

75 Set balance 7575

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 10 / 27

Page 38: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Race conditions

(define (withdraw amount)(if (>= balance amount)

(begin (set! balance (- balance amount))balance)

"Insufficient funds"))((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

Access balance (100) 100100 Access balance (100)

New value 100 - 10 = 90 100100 New value 100 - 25 = 75

Set balance 90 9075 Set balance 75

75

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 10 / 27

Page 39: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Race conditions

(define (withdraw amount)(if (>= balance amount)

(begin (set! balance (- balance amount))balance)

"Insufficient funds"))((alex ’withdraw) 10)((ben ’withdraw) 25)

Alex Bank Ben100

Access balance (100) 100100 Access balance (100)

New value 100 - 10 = 90 100100 New value 100 - 25 = 75

Set balance 90 9075 Set balance 7575

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 10 / 27

Page 40: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Correct behavior of concurrent programs

Require:

That no two operations that change any shared state can occur atthe same time

Guarantees correctness, but too conservative?That a concurrent system produces the same result as if theprocesses had run sequentially in some order

Does not require the processes to actually run sequentially, only toproduce results as if they had run sequentiallyThere may be more than one “correct” result as a consequence!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 11 / 27

Page 41: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Correct behavior of concurrent programs

Require:That no two operations that change any shared state can occur atthe same time

Guarantees correctness, but too conservative?That a concurrent system produces the same result as if theprocesses had run sequentially in some order

Does not require the processes to actually run sequentially, only toproduce results as if they had run sequentiallyThere may be more than one “correct” result as a consequence!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 11 / 27

Page 42: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Correct behavior of concurrent programs

Require:That no two operations that change any shared state can occur atthe same time

Guarantees correctness, but too conservative?

That a concurrent system produces the same result as if theprocesses had run sequentially in some order

Does not require the processes to actually run sequentially, only toproduce results as if they had run sequentiallyThere may be more than one “correct” result as a consequence!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 11 / 27

Page 43: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Correct behavior of concurrent programs

Require:That no two operations that change any shared state can occur atthe same time

Guarantees correctness, but too conservative?That a concurrent system produces the same result as if theprocesses had run sequentially in some order

Does not require the processes to actually run sequentially, only toproduce results as if they had run sequentiallyThere may be more than one “correct” result as a consequence!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 11 / 27

Page 44: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Correct behavior of concurrent programs

Require:That no two operations that change any shared state can occur atthe same time

Guarantees correctness, but too conservative?That a concurrent system produces the same result as if theprocesses had run sequentially in some order

Does not require the processes to actually run sequentially, only toproduce results as if they had run sequentiallyThere may be more than one “correct” result as a consequence!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 11 / 27

Page 45: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 46: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1

b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 47: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1

c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 48: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 49: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 50: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2

e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 51: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 52: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 53: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 54: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e

10 10 100 100 101

a b d c e

10 10 10 100 11

a d b c e

10 10 10 100 11

d a b c e

10 10 10 100 11

a b d e c

10 10 10 11 100

a d b e c

10 10 10 11 100

d a b e c

10 10 10 11 100

a d e b c

10 10 11 11 110

d a e b c

10 10 11 11 110

d e a b c

10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 55: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define f1 (lambda () (set! x (* x x))))(define f2 (lambda () (set! x (+ x 1))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

Internal orderpreserved

f2d – Look up x in f2e – Assign sum of d and 1 to x

Internal orderpreserved

a b c d e 10 10 100 100 101a b d c e 10 10 10 100 11a d b c e 10 10 10 100 11d a b c e 10 10 10 100 11a b d e c 10 10 10 11 100

a d b e c 10 10 10 11 100d a b e c 10 10 10 11 100a d e b c 10 10 11 11 110d a e b c 10 10 11 11 110d e a b c 10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 12 / 27

Page 56: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serializing access to shared state

Processes will execute concurrently, but there will certain sets ofprocedures such that only one execution of a procedure in eachset is permitted to happen at a time

If some procedure in the set is being executed, then any otherprocess that attempts to execute any procedure in the set will beforced to wait until the first execution has finishedUse serialization to control access to shared variables

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 13 / 27

Page 57: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serializing access to shared state

Processes will execute concurrently, but there will certain sets ofprocedures such that only one execution of a procedure in eachset is permitted to happen at a timeIf some procedure in the set is being executed, then any otherprocess that attempts to execute any procedure in the set will beforced to wait until the first execution has finished

Use serialization to control access to shared variables

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 13 / 27

Page 58: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serializing access to shared state

Processes will execute concurrently, but there will certain sets ofprocedures such that only one execution of a procedure in eachset is permitted to happen at a timeIf some procedure in the set is being executed, then any otherprocess that attempts to execute any procedure in the set will beforced to wait until the first execution has finishedUse serialization to control access to shared variables

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 13 / 27

Page 59: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serializers “mark” critical regions

We can mark regions of code that cannot overlap execution intime. This adds an additional constraint to the partial orderingimposed by the separate processes

Assume make-serializer returns a procedure that takes aprocedure as input and returns a serialized procedure thatbehaves like the original, except that if another procedure in thesame serialized set is underway, this procedure must waitWhere do we put the serializers?

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 14 / 27

Page 60: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serializers “mark” critical regions

We can mark regions of code that cannot overlap execution intime. This adds an additional constraint to the partial orderingimposed by the separate processesAssume make-serializer returns a procedure that takes aprocedure as input and returns a serialized procedure thatbehaves like the original, except that if another procedure in thesame serialized set is underway, this procedure must wait

Where do we put the serializers?

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 14 / 27

Page 61: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serializers “mark” critical regions

We can mark regions of code that cannot overlap execution intime. This adds an additional constraint to the partial orderingimposed by the separate processesAssume make-serializer returns a procedure that takes aprocedure as input and returns a serialized procedure thatbehaves like the original, except that if another procedure in thesame serialized set is underway, this procedure must waitWhere do we put the serializers?

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 14 / 27

Page 62: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define kelloggs (make-serializer))(define f1 (kelloggs (lambda () (set! x (* x x)))))(define f2 (kelloggs (lambda () (set! x (+ x 1)))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

f2d – Look up x in f2e – Assign sum of d and 1 to x

a b c d e 10 10 100 100 101 d e a b c 10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 15 / 27

Page 63: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define kelloggs (make-serializer))(define f1 (kelloggs (lambda () (set! x (* x x)))))(define f2 (kelloggs (lambda () (set! x (+ x 1)))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

f2d – Look up x in f2e – Assign sum of d and 1 to x

a b c d e 10 10 100 100 101 d e a b c 10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 15 / 27

Page 64: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define x 10)(define kelloggs (make-serializer))(define f1 (kelloggs (lambda () (set! x (* x x)))))(define f2 (kelloggs (lambda () (set! x (+ x 1)))))

(parallel-execute f1 f2)

f1a – Look up first x in f1b – Look up second x in f1c – Assign product of a and b to x

f2d – Look up x in f2e – Assign sum of d and 1 to x

a b c d e 10 10 100 100 101 d e a b c 10 11 11 11 121

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 15 / 27

Page 65: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

(define (make-account balance)(define (withdraw amount)

(if (>= balance amount)(begin (set! balance (- balance amount))

balance)"Insufficient funds"))

(define (deposit amount)(set! balance (+ balance amount)))

(let ((kelloggs (make-serializer)))(define (dispatch msg)(cond ((eq? msg ’withdraw) (kelloggs withdraw))

((eq? msg ’deposit) (kelloggs deposit))((eq? msg ’balance) balance)(else (error "unknown request" msg)))))

dispatch)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 16 / 27

Page 66: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1004. Difference (- alex mpp) = 2005. Withdraw 200 from alex (has 100)6. Deposit 200 into mpp (has 300)2. Withdraw 100 from alex (has 0)3. Deposit 100 into ben (has 300)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 67: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 100

2. Withdraw 100 from alex (has 200)3. Deposit 100 into ben (has 300)4. Difference (- alex mpp) = 1005. Withdraw 100 from alex (has 100)6. Deposit 100 into mpp (has 200)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 68: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1002. Withdraw 100 from alex (has 200)

3. Deposit 100 into ben (has 300)4. Difference (- alex mpp) = 1005. Withdraw 100 from alex (has 100)6. Deposit 100 into mpp (has 200)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 69: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1002. Withdraw 100 from alex (has 200)3. Deposit 100 into ben (has 300)

4. Difference (- alex mpp) = 1005. Withdraw 100 from alex (has 100)6. Deposit 100 into mpp (has 200)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 70: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1002. Withdraw 100 from alex (has 200)3. Deposit 100 into ben (has 300)4. Difference (- alex mpp) = 100

5. Withdraw 100 from alex (has 100)6. Deposit 100 into mpp (has 200)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 71: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1002. Withdraw 100 from alex (has 200)3. Deposit 100 into ben (has 300)4. Difference (- alex mpp) = 1005. Withdraw 100 from alex (has 100)

6. Deposit 100 into mpp (has 200)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 72: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1002. Withdraw 100 from alex (has 200)3. Deposit 100 into ben (has 300)4. Difference (- alex mpp) = 1005. Withdraw 100 from alex (has 100)6. Deposit 100 into mpp (has 200)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 73: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 100

4. Difference (- alex mpp) = 2005. Withdraw 200 from alex (has 100)6. Deposit 200 into mpp (has 300)2. Withdraw 100 from alex (has 0)3. Deposit 100 into ben (has 300)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 74: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1004. Difference (- alex mpp) = 200

5. Withdraw 200 from alex (has 100)6. Deposit 200 into mpp (has 300)2. Withdraw 100 from alex (has 0)3. Deposit 100 into ben (has 300)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 75: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1004. Difference (- alex mpp) = 2005. Withdraw 200 from alex (has 100)

6. Deposit 200 into mpp (has 300)2. Withdraw 100 from alex (has 0)3. Deposit 100 into ben (has 300)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 76: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1004. Difference (- alex mpp) = 2005. Withdraw 200 from alex (has 100)6. Deposit 200 into mpp (has 300)

2. Withdraw 100 from alex (has 0)3. Deposit 100 into ben (has 300)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 77: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1004. Difference (- alex mpp) = 2005. Withdraw 200 from alex (has 100)6. Deposit 200 into mpp (has 300)2. Withdraw 100 from alex (has 0)

3. Deposit 100 into ben (has 300)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 78: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Multiple shared resources

(define (exchange account1 account2)(let ((difference (- (account1 ’balance)

(account2 ’balance))))((account1 ’withdraw) difference)((account2 ’deposit) difference)))

(parallel-execute (lambda () (exchange alex ben))(lambda () (exchange alex mpp)))

; alex = 300, ben = 200, mpp = 100

1. Difference (- alex ben) = 1004. Difference (- alex mpp) = 2005. Withdraw 200 from alex (has 100)6. Deposit 200 into mpp (has 300)2. Withdraw 100 from alex (has 0)3. Deposit 100 into ben (has 300)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 17 / 27

Page 79: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serializing object access

(define (make-account balance)(define (withdraw amount)

(if (>= balance amount)(begin (set! balance (- balance amount))

balance)"Insufficient funds"))

(define (deposit amount)(set! balance (+ balance amount)))

(let ((kelloggs (make-serializer)))(define (dispatch msg)(cond ((eq? msg ’withdraw) withdraw)

((eq? msg ’deposit) deposit)((eq? msg ’balance) balance)((eq? msg ’serializer) kelloggs)(else (error "unknown request" msg))))

dispatch))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 18 / 27

Page 80: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serialize access to all variables

(define (deposit account amount)(let ((s (account ’serializer))

(d (account ’deposit)))((s d) amount)))

(define (serialized-exchange acct1 acct2)(let ((serializer1 (acct1 ’serializer))

(serializer2 (acct2 ’serializer)))((serializer1 (serializer2 exchange))acct1acct2)))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 19 / 27

Page 81: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serialize access to all variables

(define (deposit account amount)(let ((s (account ’serializer))

(d (account ’deposit)))((s d) amount)))

(define (serialized-exchange acct1 acct2)(let ((serializer1 (acct1 ’serializer))

(serializer2 (acct2 ’serializer)))((serializer1 (serializer2 exchange))acct1acct2)))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 19 / 27

Page 82: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Suppose Alex attempts to exchange a1 with a2

And Ben attempts to exchange a2 with a1

Imagine that Alex gets the serializer for a1 at the same time thatBen gets the serializer for a2.Now Alex is stalled waiting for the serializer from a2, but Ben isholding it.And Ben is similarly waiting for the serializer from a1, but Alex isholding it.

DEADLOCK!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 20 / 27

Page 83: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Suppose Alex attempts to exchange a1 with a2

And Ben attempts to exchange a2 with a1

Imagine that Alex gets the serializer for a1 at the same time thatBen gets the serializer for a2.Now Alex is stalled waiting for the serializer from a2, but Ben isholding it.And Ben is similarly waiting for the serializer from a1, but Alex isholding it.

DEADLOCK!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 20 / 27

Page 84: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Suppose Alex attempts to exchange a1 with a2

And Ben attempts to exchange a2 with a1

Imagine that Alex gets the serializer for a1 at the same time thatBen gets the serializer for a2.

Now Alex is stalled waiting for the serializer from a2, but Ben isholding it.And Ben is similarly waiting for the serializer from a1, but Alex isholding it.

DEADLOCK!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 20 / 27

Page 85: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Suppose Alex attempts to exchange a1 with a2

And Ben attempts to exchange a2 with a1

Imagine that Alex gets the serializer for a1 at the same time thatBen gets the serializer for a2.Now Alex is stalled waiting for the serializer from a2, but Ben isholding it.

And Ben is similarly waiting for the serializer from a1, but Alex isholding it.

DEADLOCK!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 20 / 27

Page 86: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Suppose Alex attempts to exchange a1 with a2

And Ben attempts to exchange a2 with a1

Imagine that Alex gets the serializer for a1 at the same time thatBen gets the serializer for a2.Now Alex is stalled waiting for the serializer from a2, but Ben isholding it.And Ben is similarly waiting for the serializer from a1, but Alex isholding it.

DEADLOCK!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 20 / 27

Page 87: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Suppose Alex attempts to exchange a1 with a2

And Ben attempts to exchange a2 with a1

Imagine that Alex gets the serializer for a1 at the same time thatBen gets the serializer for a2.Now Alex is stalled waiting for the serializer from a2, but Ben isholding it.And Ben is similarly waiting for the serializer from a1, but Alex isholding it.

DEADLOCK!

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 20 / 27

Page 88: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Deadlocks

Classic deadlock case: Dining Philosophers problem

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 21 / 27

Page 89: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Deadlocks

Classic deadlock case: Dining Philosophers problem

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 21 / 27

Page 90: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Implementation time

Racket has concurrency, using threads

We can implement serializers using a primitive synchronizationmethod, called a semaphoreA semaphore counts the number of available resourcesSemaphores can be atomically incremented and decrementedAttempts to decrement when there are no available resourcescauses the thread to blockIf the semaphore has only up to one resource, it is a mutex(“mutual exclusion”)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 22 / 27

Page 91: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Implementation time

Racket has concurrency, using threadsWe can implement serializers using a primitive synchronizationmethod, called a semaphore

A semaphore counts the number of available resourcesSemaphores can be atomically incremented and decrementedAttempts to decrement when there are no available resourcescauses the thread to blockIf the semaphore has only up to one resource, it is a mutex(“mutual exclusion”)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 22 / 27

Page 92: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Implementation time

Racket has concurrency, using threadsWe can implement serializers using a primitive synchronizationmethod, called a semaphoreA semaphore counts the number of available resources

Semaphores can be atomically incremented and decrementedAttempts to decrement when there are no available resourcescauses the thread to blockIf the semaphore has only up to one resource, it is a mutex(“mutual exclusion”)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 22 / 27

Page 93: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Implementation time

Racket has concurrency, using threadsWe can implement serializers using a primitive synchronizationmethod, called a semaphoreA semaphore counts the number of available resourcesSemaphores can be atomically incremented and decremented

Attempts to decrement when there are no available resourcescauses the thread to blockIf the semaphore has only up to one resource, it is a mutex(“mutual exclusion”)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 22 / 27

Page 94: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Implementation time

Racket has concurrency, using threadsWe can implement serializers using a primitive synchronizationmethod, called a semaphoreA semaphore counts the number of available resourcesSemaphores can be atomically incremented and decrementedAttempts to decrement when there are no available resourcescauses the thread to block

If the semaphore has only up to one resource, it is a mutex(“mutual exclusion”)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 22 / 27

Page 95: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Implementation time

Racket has concurrency, using threadsWe can implement serializers using a primitive synchronizationmethod, called a semaphoreA semaphore counts the number of available resourcesSemaphores can be atomically incremented and decrementedAttempts to decrement when there are no available resourcescauses the thread to blockIf the semaphore has only up to one resource, it is a mutex(“mutual exclusion”)

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 22 / 27

Page 96: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Serializer

(define (make-serializer)(let ((mutex (make-mutex)))(lambda (p)

(lambda args(mutex ’acquire)(let ((val (apply p args)))

(mutex ’release)val)))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 23 / 27

Page 97: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Mutex

(define (make-mutex)(let ((sema (make-semaphore 1)))(lambda (m)(cond ((eq? m ’acquire) (semaphore-wait sema))

((eq? m ’release) (semaphore-post sema))(else (error "Invalid argument:" m))))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 24 / 27

Page 98: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Mutex

(define (make-mutex)(let ((sema (make-semaphore 1)))(lambda (m)(cond ((eq? m ’acquire) (semaphore-wait sema))

((eq? m ’release) (semaphore-post sema))(else (error "Invalid argument:" m))))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 24 / 27

Page 99: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Mutex

(define (make-mutex)(let ((sema (make-semaphore 1)))(lambda (m)(cond ((eq? m ’acquire) (semaphore-wait sema))

((eq? m ’release) (semaphore-post sema))(else (error "Invalid argument:" m))))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 24 / 27

Page 100: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Mutex

(define (make-mutex)(let ((sema (make-semaphore 1)))(lambda (m)(cond ((eq? m ’acquire) (semaphore-wait sema))

((eq? m ’release) (semaphore-post sema))(else (error "Invalid argument:" m))))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 24 / 27

Page 101: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Parallel evaluation

(define (parallel-execute p1 p2)(let* ((parent (current-thread))

(t1 (thread (send-parent p1 parent)))(t2 (thread (send-parent p2 parent))))

(list (thread-receive) (thread-receive))))

(define (send-parent f parent)(lambda () (thread-send parent (f))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 25 / 27

Page 102: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Parallel evaluation

(define (parallel-execute p1 p2)(let* ((parent (current-thread))

(t1 (thread (send-parent p1 parent)))(t2 (thread (send-parent p2 parent))))

(list (thread-receive) (thread-receive))))

(define (send-parent f parent)(lambda () (thread-send parent (f))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 25 / 27

Page 103: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Parallel evaluation

(define (parallel-execute p1 p2)(let* ((parent (current-thread))

(t1 (thread (send-parent p1 parent)))(t2 (thread (send-parent p2 parent))))

(list (thread-receive) (thread-receive))))

(define (send-parent f parent)(lambda () (thread-send parent (f))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 25 / 27

Page 104: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Parallel evaluation

(define (parallel-execute p1 p2)(let* ((parent (current-thread))

(t1 (thread (send-parent p1 parent)))(t2 (thread (send-parent p2 parent))))

(list (thread-receive) (thread-receive))))

(define (send-parent f parent)(lambda () (thread-send parent (f))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 25 / 27

Page 105: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Parallel evaluation

(define (parallel-execute p1 p2)(let* ((parent (current-thread))

(t1 (thread (send-parent p1 parent)))(t2 (thread (send-parent p2 parent))))

(list (thread-receive) (thread-receive))))

(define (send-parent f parent)(lambda () (thread-send parent (f))))

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 25 / 27

Page 106: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Thread communication

Shared object state is implicit thread communication

More formal: thread-send and thread-receive are a simpleasynchronous message queueInter-process communication through events

Sporadic requests to process somethingEvents are a good model for user interaction

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 26 / 27

Page 107: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Thread communication

Shared object state is implicit thread communicationMore formal: thread-send and thread-receive are a simpleasynchronous message queue

Inter-process communication through eventsSporadic requests to process somethingEvents are a good model for user interaction

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 26 / 27

Page 108: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Thread communication

Shared object state is implicit thread communicationMore formal: thread-send and thread-receive are a simpleasynchronous message queueInter-process communication through events

Sporadic requests to process somethingEvents are a good model for user interaction

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 26 / 27

Page 109: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Thread communication

Shared object state is implicit thread communicationMore formal: thread-send and thread-receive are a simpleasynchronous message queueInter-process communication through events

Sporadic requests to process something

Events are a good model for user interaction

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 26 / 27

Page 110: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Thread communication

Shared object state is implicit thread communicationMore formal: thread-send and thread-receive are a simpleasynchronous message queueInter-process communication through events

Sporadic requests to process somethingEvents are a good model for user interaction

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 26 / 27

Page 111: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency in large systems

It’s hard:

Coarse-grained locking is inefficient, but ensures correct behaviorFine-grained locking can be efficient, but is bug-prone and brittle tonew kinds of operationsLocks break abstraction barriersOpens up whole new classes of bugs (race conditions, deadlock,livelock, etc.)These issues are recursiveAlso, very irritating to debug – non-deterministic, debugging outputintermixed between threads, heisenbugs, etc.

But we need to do it

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 27 / 27

Page 112: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency in large systems

It’s hard:Coarse-grained locking is inefficient, but ensures correct behavior

Fine-grained locking can be efficient, but is bug-prone and brittle tonew kinds of operationsLocks break abstraction barriersOpens up whole new classes of bugs (race conditions, deadlock,livelock, etc.)These issues are recursiveAlso, very irritating to debug – non-deterministic, debugging outputintermixed between threads, heisenbugs, etc.

But we need to do it

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 27 / 27

Page 113: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency in large systems

It’s hard:Coarse-grained locking is inefficient, but ensures correct behaviorFine-grained locking can be efficient, but is bug-prone and brittle tonew kinds of operations

Locks break abstraction barriersOpens up whole new classes of bugs (race conditions, deadlock,livelock, etc.)These issues are recursiveAlso, very irritating to debug – non-deterministic, debugging outputintermixed between threads, heisenbugs, etc.

But we need to do it

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 27 / 27

Page 114: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency in large systems

It’s hard:Coarse-grained locking is inefficient, but ensures correct behaviorFine-grained locking can be efficient, but is bug-prone and brittle tonew kinds of operationsLocks break abstraction barriers

Opens up whole new classes of bugs (race conditions, deadlock,livelock, etc.)These issues are recursiveAlso, very irritating to debug – non-deterministic, debugging outputintermixed between threads, heisenbugs, etc.

But we need to do it

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 27 / 27

Page 115: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency in large systems

It’s hard:Coarse-grained locking is inefficient, but ensures correct behaviorFine-grained locking can be efficient, but is bug-prone and brittle tonew kinds of operationsLocks break abstraction barriersOpens up whole new classes of bugs (race conditions, deadlock,livelock, etc.)

These issues are recursiveAlso, very irritating to debug – non-deterministic, debugging outputintermixed between threads, heisenbugs, etc.

But we need to do it

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 27 / 27

Page 116: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency in large systems

It’s hard:Coarse-grained locking is inefficient, but ensures correct behaviorFine-grained locking can be efficient, but is bug-prone and brittle tonew kinds of operationsLocks break abstraction barriersOpens up whole new classes of bugs (race conditions, deadlock,livelock, etc.)These issues are recursive

Also, very irritating to debug – non-deterministic, debugging outputintermixed between threads, heisenbugs, etc.

But we need to do it

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 27 / 27

Page 117: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency in large systems

It’s hard:Coarse-grained locking is inefficient, but ensures correct behaviorFine-grained locking can be efficient, but is bug-prone and brittle tonew kinds of operationsLocks break abstraction barriersOpens up whole new classes of bugs (race conditions, deadlock,livelock, etc.)These issues are recursiveAlso, very irritating to debug – non-deterministic, debugging outputintermixed between threads, heisenbugs, etc.

But we need to do it

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 27 / 27

Page 118: Concurrency - 6.037 - Structure and Interpretation of ...web.mit.edu/alexmv/6.037/l8a-transitions.pdf · Concurrency 6.037 - Structure and Interpretation of Computer Programs Mike

Concurrency in large systems

It’s hard:Coarse-grained locking is inefficient, but ensures correct behaviorFine-grained locking can be efficient, but is bug-prone and brittle tonew kinds of operationsLocks break abstraction barriersOpens up whole new classes of bugs (race conditions, deadlock,livelock, etc.)These issues are recursiveAlso, very irritating to debug – non-deterministic, debugging outputintermixed between threads, heisenbugs, etc.

But we need to do it

Mike Phillips <mpp> (MIT) Concurrency Lecture 8A 27 / 27