77
03/27/22 CS2104, Lecture 8 1 Programming Language Concepts, COSC-3308- 01 Lecture 8 (Multiple) Threads Semantics

9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

Embed Size (px)

Citation preview

Page 1: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 1

Programming Language Concepts, COSC-3308-01Lecture 8  

(Multiple) Threads Semantics

Page 2: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 2

Reminder of the Last Lecture

abstract data types declarative concurrency design a concurrent program execution of concurrent programs

Page 3: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 3

Overview

Thread Semantics Extend the abstract machine to execute

multiple threads

Page 4: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 4

Thread Semantics

Page 5: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 5

Semantics for Threads. (Interleaving Semantics)

Interleaving: two different threads can interleave their computation steps

Interleaving semantics: Language viewpoint:

threads can do interleaving execution; each computation step is atomic.

Implementation viewpoint: might execute several threads in parallel, however must

execute as if one thread at a time; that is, whatever the parallel execution is, there is always at

least one interleaving that is observationally equivalent to it.

Page 6: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 6

Causal Order of Computation Steps For a given program, all computation steps

form a partial order, called the causal order. A computation step occurs before another

step if in all possible executions of the program it happens before the other.

Similarly for a computation step that occurs after another step.

Sometimes a step is neither before nor after another step, that is, the two steps are concurrent.

Page 7: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 7

Causal Order and Interleaving Executions

Page 8: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 8

Declarative Nondeterminism Nondeterministic execution: there is an execution

state in which there is a choice of what to do next, i.e., a choice of which thread to reduce.

Example: After Ia, there is a choice of either I1 or Ib Declarative concurrent model: the nondeterminism isn’t

visible. dataflow variables can be bound to only one value. The

nondeterminism affects only the exact moment when each binding takes place; it does not affect the plain fact that the binding does take place.

any operation that needs the value of a variable has no choice but to wait until the variable is bound.

Page 9: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 9

Semantics for Threads (Scheduling) The choice of which thread to execute next is done by

the scheduler. A thread is ready (or runnable), if its statement has

all the information it needs to execute at least one computation step.

Once a thread is ready, it stays ready indefinitely. So, thread reduction in declarative concurrent model

is monotonic. A thread is suspended (or blocked), if it is not ready.

That is, its first statement cannot continue because it does not have all the information it needs.

Page 10: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 10

Monotonicity (Example)

thread

if B then … else … end

end

When B is bound, thread will eventually run When B is bound, the value of B is fixed

value of B is independent of when thread executes

Page 11: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 11

Monotonicity: Simplicity

Result is scheduling independent unless attempt to put inconsistent information to

store Example: non-determinism (failure exception)thread X=1 end

thread X=2 end which value for X?

Different with explicit mutable state (JAVA) if variable values changed over time, result would

depend on order in which threads run

Page 12: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 12

Dependencies Suspension and resumption driven by variable

bindings Progress, only if for each variable, its value is

supplied Typical error-scenario: deadlock

thread depends on X, supposed to bind Y thread depends on Y, supposed to bind X

Example:declare X Y

thread if X==1 then Y=1 {Browse 1} end end

thread if Y==1 then X=1 {Browse 2} end end

Page 13: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 13

Extend Abstract Machine

Extend to execute multiple threads shared store: all threads share common

store semantic stack: corresponds to a thread thread creation: create a new semantic stack

Orthogonal: scheduling policy scheduling policy: which thread to execute? consider only non-suspended threads!

Page 14: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 14

Abstract Machine Concepts (Reminder) Single-assignment store Environment Semantic statement Execution state Computation

Page 15: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 15

Abstract Machine (Reminder)

Performs a computation Computation is a sequence of execution states Execution state

stack of semantic statements single assignment store

Semantic statement statement environment

Environment maps variable identifiers to store entities

Page 16: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 16

Single Assignment Store (Reminder) Single assignment store

set of store variables partitioned into

sets of variables that are equal but unbound variables bound to value

Example store {x1, x2=x3, x4=a|x2} x1 unbound x2, x3 equal and unbound x4 bound to partial value a|x2

Page 17: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 17

Environment (Reminder)

Environment E maps variable identifiers to entities in store written as set of pairs X x

variable identifier X store variable x

Example environment { X x, Y y } maps identifier X to store variable x maps identifier Y to store variable y

Page 18: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 18

Environment and Store (Reminder) Given: environment E, store Looking up value for variable identifier X:

find store variable in environment E(X) take value from for E(X)

Example: ={x1, x2=x3, x4=a|x2} E = { X x1, Y x4 }

E(X) = x1 and no information in on x1

E(Y) = x4 and binds x4 to a|x2

Page 19: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 19

Environment Adjunction (Reminder) Given: Environment E

E + {x1x1, …, xnxn}

is new environment E ’ with mappings added: always take store entity from new mappings might overwrite old mappings

Page 20: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 20

Semantic Statements (Reminder) To actually execute statement:

environment to map identifiers modified with execution of each statement each statement has its own environment

store to find values all statements modify same store single store

Semantic statement ( s, E ) pair of (statement, environment)

Page 21: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 21

Semantic Stack (Reminder)

Execution maintains stack of semantic statements ST

[(s1, E1), …, (sn, En)] always topmost statement (s1, E1) executes first rest of stack: what needs to be done

Page 22: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 22

Semantic Stack States (Reminder) Semantic stack can be in run-time states

terminated stack is empty runnable can do execution step suspended stack not empty, no execution

step possible

Statements non-suspending can always execute suspending need values from store

dataflow behavior

Page 23: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 23

Summary

Single assignment store Environments E

adjunction E + {…} Semantic statements (s, E) Semantic stacks [(s, E) … ] Execution state (ST, ) Program execution

runnable, terminated, suspended Statements

suspending, non-suspending

Page 24: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 24

Executing if (Reminder)

Semantic statement is

(if x then s1 else s2 end, E)

If activation condition “x bound” is true if E(x) bound to true push s1

if E(x) bound to false push s2

otherwise, raise error

Otherwise, suspend…

Page 25: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 25

Procedure Call (Reminder)

Semantic statement is

({x y1 … yn}, E)

where E(x) is to be called y1, …, yn are actual parameters

Suspending statement, suspension condition E(x) is determined

Page 26: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 26

Summary so far

Semantic statement executes by popping itself always creating environment local manipulating store local, = pushing new statements local, if

sequential composition Semantic statement can suspend

activation condition if, {…}, case read store

Page 27: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 27

Multiple Semantic Stacks

Abstract machine has multiple semantic stacks each semantic stack represents one thread

Number of semantic stacks change over time increases: new threads created decreases: threads terminate

Page 28: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 28

Multisets Collection of semantic stacks called

multiset of semantic stacks

Multiset: like a set, but maintains multiplicity ordinary set: element can be contained at most once

{1,2,3} multiset: element can be contained many times

{1,1,1,2,2,3} is different from {1,1,2,3,3} just think of: bag, collection, bunch of something same thread is allowed to occur more than once

Page 29: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 29

Execution State

( Multiset of semantic stacks, store )

( { ST1, …, STn }, )

we write multisets with normal set parentheses: { and }

Page 30: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 30

Execution State

( Multiset of semantic stacks, store )

( { ST1, …, STn }, )

MST Multisets of stacks are denoted MST

Page 31: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 31

Initial Execution State

Given statement s, start execution as before with empty environment, empty store and just one thread

( { [ (s, ) ] }, )

Page 32: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 32

Initial Execution State

Given statement s, start execution as before with empty environment, empty store and just one thread

( { [ (s, ) ] }, )

execution state

Page 33: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 33

Initial Execution State

Given statement s, start execution as before with empty environment, empty store and just one thread

( { [ (s, ) ] }, )

store (empty)

Page 34: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 34

Initial Execution State

Given statement s, start execution as before with empty environment, empty store and just one thread

( { [ (s, ) ] }, )

multiset of threads

(just one)

Page 35: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 35

Initial Execution State

Given statement s, start execution as before with empty environment, empty store and just one thread

( { [ (s, ) ] }, )

thread (just one)

Page 36: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 36

Initial Execution State

Given statement s, start execution as before with empty environment, empty store and just one thread

( { [ (s, ) ] }, )

semantic statement

Page 37: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 37

Initial Execution State

Given statement s, start execution as before with empty environment, empty store and just one thread

( { [ (s, ) ] }, )

statement (initial)

Page 38: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 38

Initial Execution State

Given statement s, start execution as before with empty environment, empty store and just one thread

( { [ (s, ) ] }, )

empty environment

Page 39: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 39

Execution

Execution steps

(MST1, 1) (MST2, 2) …

At each step select runnable semantic stack STi from MSTi

execute topmost semantic statement of STi resulting in ST’i

continue with threads MSTi+1 = {ST’i} (MSTi – {STi})

Page 40: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 40

Execution

Execution steps

(MST1, 1) (MST2, 2) …

At each step select runnable semantic stack STi from MSTi

execute topmost semantic statement of STi resulting in ST’i

continue with threadsMSTi+1 = {ST’i} (MSTi – {STi})

Page 41: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 41

Statements

s ::= skip

| x = y | x = v

| s1 s2

| local x in s end | if x then s1 else s2 end

| {x y1 … yn}

Page 42: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 42

Statements with Thread Creations ::= skip

| x = y | x = v

| s1 s2

| local x in s end | if x then s1 else s2 end

| {x y1 … yn}

| thread s end

Page 43: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 43

Sketch of Computation

Multiple threads sharing store

ST1 STn…

Page 44: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 44

Sketch of Computation

Thread creation statement…

STn…STi

(threadsend,E)

Page 45: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 45

Sketch of Computation

…new semantic stack running s

STn…STi

(s,E)

Page 46: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 46

Example

local B X in

thread

if B then X=1 else X=2 end

end

B=true

end

Page 47: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 47

The Abstract Machine with Threads( { [ (local B X in thread if B then X=1 else X=2 end end B=true

end, ) ] }, ) We need to apply the local statement

Page 48: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 48

The Abstract Machine with Threads( { [ (thread if B then X=1 else X=2 end

end

B=true,

{Bb, Xx}) ]

},

{b, x} )

We need to apply the sequential composition

Page 49: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 49

The Abstract Machine with Threads( { [ (thread if B then X=1 else X=2 end

end, {Bb, Xx}) (B=true, {Bb, Xx}) ] }, {b, x} )

We need to apply thread … end

Page 50: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 50

The Abstract Machine with Threads( { [ (if B then X=1 else X=2 end, {Bb, Xx}) ]

[ (B=true, {Bb, Xx})

]

},

{b, x} )

We need to select a runnable semantic stack from the

multiset of semantic stacks First semantic stack cannot run because B is unbound!

Page 51: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 51

The Abstract Machine with Threads( { [ (if B then X=1 else X=2 end, {Bb, Xx}) ] [ ] }, {b=true, x} )

After we run the second semantic stack The first semantic stack can run now because

the if statement is not suspended anymore!

Page 52: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 52

The Abstract Machine with Threads( { [ (X=1, {Bb, Xx})

]

[

]

},

{b=true, x} )

We need to execute the variable-value equality

Page 53: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 53

The Abstract Machine with Threads( { [ ] [ ] }, {b=true, x=1} )

We conclude that B and X have been bound to true and 1, respectively.

Page 54: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 54

Statements with Thread Creations ::= skip

| x = y | x = v

| s1 s2

| local x in s end | if x then s1 else s2 end

| {x y1 … yn}

| thread s end | {ByNeed x y}

Page 55: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 55

Sketch of Computation

Thread creation statement…

STn…STi

({ByNeed x y},E)

Page 56: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 56

Sketch of Computation

The store is the variable-store + the trigger-store

STn…STi

({ByNeed x y},E)

Page 57: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 57

Triggers

A by-need trigger is a pair trig(F, X): a zero-argument function F a variable X

Additionally to single-assignment store , we consider a new store , called the trigger-store

The trigger store contains all by-need triggers and is initially empty.

The execution state becomes a triple (MST, , ).

Page 58: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 58

Trigger Creation (Executing ByNeed) Semantic statement is

({ByNeed x y} , E) x is a one-argument procedure y is a variable

If y is not determined (unbound) Add the trigger trig(E(x), E(y)) to the trigger store

If y is determined (bound) Create a new thread with initial semantic stack

[({x y}, E)]

Page 59: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 59

Trigger Activation (Executing ByNeed) If trig(x, y) is in the trigger-store and a need

on y is detected, then there is either a thread that is suspended waiting

for y to be determined, or an attempt to bind y to make it determined

then Remove the trigger from the trigger store. Create a new thread with initial semantic stack:

[ ({<x> <y>},{<x>x, <y>y}) ] {…} is a procedure call!

Page 60: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 60

The Abstract Machine with By-Need( { [ (local X Z in X={ByNeed fun {$} 4 end} {Browse X} Z=X+1

end, ) ] }, , ) is the trigger-store We need to apply the local statement

Page 61: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 61

The Abstract Machine with By-Need( { [ (X={ByNeed fun {$} 4 end} {Browse X}

Z=X+1, {Xx, Zz}) ] }, {x, z} , )

We need to apply the sequential composition

Page 62: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 62

The Abstract Machine with By-Need( { [ (X={ByNeed fun {$} 4 end}, {Xx, Zz}) ({Browse X} Z=X+1, {Xx, Zz}) ] }, {x, z} , )

We need to apply the trigger creation

Page 63: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 63

The Abstract Machine with By-Need( { [({Browse X}

Z=X+1,

{Xx, Zz})

]

},

{x, z} , trig(fun {$} 4 end, x))

We need to apply the sequential composition

Page 64: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 64

The Abstract Machine with By-Need( { [({Browse X}, {Xx, Zz}) (Z=X+1, {Xx, Zz}) ] }, {x, z} , trig(fun {$} 4 end, x))

Browse visualizes the single-assignment store in the Browser window

In fact, Browse is running in a system’s thread!

Page 65: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 65

The Abstract Machine with By-Need( { [({Browse X}, {Xx, Zz})] [(Z=X+1, {Xx, Zz})] }, {x, z} , trig(fun {$} 4 end, x))

From this multiset of threads, Browse is suspended since X is unbound

Executing the second semantic stack, X is needed because it appears in the right-hand side of Z=X+1, so trig(…, x) is removed from and we create a new thread

Page 66: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 66

The Abstract Machine with By-Need( { [({fun {$} 4 end X},{Xx})] [({Browse X}, {Xx, Zz})] [(Z=X+1, {Xx, Zz})] }, {x, z} , )

The second and third semantic stacks are suspended

We executed the first semantic stack

Page 67: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 67

Zero-Argument Function’s Call

local X in

{fun {$} 4 end X}

{Browse X}

end

X is an output argument for an anonymous procedure

4 Is equivalent withlocal X Fun1 in

proc {Fun1 Result1}

Result1 = 4

end

{Fun1 X}

{Browse X}

end

Page 68: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 68

The Abstract Machine with By-Need( {[({Browse X}, {Xx, Zz})]

[(Z=X+1, {Xx, Zz})]

},

{x=4, z} , )

Any thread can run now If executing the first semantic stack, then the

browser will display 4 If executing the second semantic stack, then …

Page 69: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 69

The Abstract Machine with By-Need( {

},

{x=4, z=5} , )

The execution stops by binding x to 4 and z to 5

Page 70: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 70

A Variable is Needed if Must be Determinedthread X={ByNeed fun {$} 3 end} end

thread Y={ByNeed fun {$} 4 end} end

thread Z=X+Y end

Considering that each thread executes atomically, there are six possible executions.

For lazy execution to be declarative, all of these executions must lead to equivalent stores.

The addition will wait until the other two triggers are created, and these triggers will then be activated.

Page 71: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 71

A Variable is Needed if it is Determinedthread X={ByNeed fun {$} 3 end} end

thread X=2 end

thread Z=X+4 end

The correct behavior of the declarative demand-driven concurrent model is that all executions should fail.

If X=2 executes last, then the trigger has already been activated, binding X to 3, so this is clear.

But if X=2 is executed first, then the trigger should also be activated.

Page 72: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 72

Garbage Collection of Threads If a thread is known to be suspended forever,

it can be garbage-collected suspends on variable not in use by any other

thread does not change semantics, just saves memory

Approximation, only straight-forward cases impossible: detect whether a thread will have no

effect! really impossible!

Page 73: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 73

Summary so far

Threads are organized as multiset of semantic stacks

Thread creation inserts new semantic stack inherits environment shares store

Thread termination removes threads

Page 74: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 74

Summary Thread Semantics Extend the abstract machine to execute

multiple threads

Page 75: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 75

Reading suggestions

From [van Roy,Haridi; 2004] Chapter 4, Sections 4.1-4.6 Exercises 4.11.10-4.11.17

Page 76: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 76

Coming up next

Stateful programming Object-Oriented Programming

Chapter 5, Sections 5.1-5.3

Page 77: 9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC-3308-01 Lecture 8 (Multiple) Threads Semantics

04/19/23 CS2104, Lecture 8 77

Thank you for your attention!

Questions?