This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS390C: Principles of Concurrency and Parallelism Principles of Concurrency and Parallelism Lecture 2: Coroutines 1/12/12 1 Thursday, January 12, 12 CS390C: Principles of Concurrency and Parallelism Expressivity ● A concurrent program provides no fundamental computability advantages over a sequential program − Any computation that can be expressed using a multitape Turing machine can be expressed using a singletape Turing machine ● Why have it, then? − Performance: parallelism − Responsiveness: ● web servers ● operating systems − Flexibility ● Algorithms, data structures, etc. − Expressivity 2 Thursday, January 12, 12 CS390C: Principles of Concurrency and Parallelism Starting Point: Control ● How do we represent or capture the notion of simultaneity? ● Example: − A generator: 3 x = 0; proc f() = { r := x; x := x + 1; return r } f(); yields 0 f(); yields 1 Thursday, January 12, 12 CS390C: Principles of Concurrency and Parallelism Generators ● Suppose instead of incrementing a counter, we returned the elements of an array ● A bit more complicated, but generation can still be expressed using updates on the array index 4 i := 0 proc f(a) = { if i < Array.length(a) then { r := A[i]; i := i + 1; return r } else raise ArrayOutofBoundsExn } Thursday, January 12, 12 CS390C: Principles of Concurrency and Parallelism Generators ● What happens if generation is not so apparent? ● Example: − Generate all the elements of a tree − How do we write a getNextTreeElt routine? ● We need to record and remember the next position in the tree for subsequent calls to the generator ● We want to keep things modular and abstract, and not expose how the tree is traversed to clients ● Want to write something like: 5 foreach node in Tree do { ... } Thursday, January 12, 12 CS390C: Principles of Concurrency and Parallelism Generators ● How do we implement foreach? − It is meant to be a generator that yields the next leaf in the tree (according to some traversal policy) every time it is invoked − Must preserve the state of the traversal internally − How should this state be kept? 6 Thursday, January 12, 12 CS390C: Principles of Concurrency and Parallelism Iterators and Coroutines ● Iterators are a special case of generators − Found in C++, Java, Python, Lua, etc....
View
Full Document
 Spring '08
 Staff
 Concurrency, coroutines

Click to edit the document details