EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman...
Transcript of EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman...
![Page 1: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/1.jpg)
EMBEDDING AND CONCURRENCY
Curt CliftonRose-Hulman Institute of Technology
SVN Update, see GoConcurrency
![Page 2: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/2.jpg)
EMBEDDING:INTERFACES AND STRUCTS
![Page 3: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/3.jpg)
EMBEDDING INTERFACES
type ReadWriter interface { Read(p []byte) (n int, err os.Error) Write(p []byte) (n int, err os.Error)}
type Reader interface { Read(p []byte) (n int, err os.Error)}
type Writer interface { Write(p []byte) (n int, err os.Error)}
type ReadWriter interface { Reader Writer}
![Page 4: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/4.jpg)
EMBEDDING STRUCTS
type Lockable struct { locked bool}
type T struct { name string Lockable}
T “inherits” Lockable’s fields and methods!
![Page 5: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/5.jpg)
CONCURRENCY PATTERNS
![Page 6: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/6.jpg)
DON’T COMMUNICATE BY SHARING MEMORY
SHARE MEMORY BY COMMUNICATING
Q1
![Page 7: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/7.jpg)
GOROUTINES
Run in parallel
Share address space
Lightweight
Multiplexed onto multiple threads automatically
![Page 8: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/8.jpg)
IDIOM: ASYNC EXECUTION
func Announce(message string, delay int64) { go func() { time.Sleep(delay) fmt.Println(message) }()}
Q2
![Page 9: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/9.jpg)
IDIOM: FUTURES
done := make(chan int)go func() { done <- list.Foldl(sum, 0)}()doSomethingForAWhile()sum <- done
Q3
![Page 10: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/10.jpg)
IDIOM: CHANNELS AS SEMAPHORES
var sem = make(chan int, MaxAllowed)
func handle(r *Request) { sem <- 1 // Blocks if MaxAllowed process calls are running process(r) // Guarded resource <-sem // Done; enable next request to run.}
func Serve(queue chan *Request) { for { req := <-queue go handle(req) }} Q4
![Page 11: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/11.jpg)
IDIOM: AVOIDING SEMAPHORES
func handle(queue chan *Request) { for r := range queue { process(r) }}
func Serve(queue chan *Request, quit chan bool) { // Start handlers for i := 0; i < MaxAllowed; i++ { go handle(queue) } <-quit // keep server running until told}
Q5
![Page 12: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/12.jpg)
IDIOM: RESOURCE POOL
var freeList = make(chan *Buffer, 100)var serverChan = make(chan *Buffer)
func client() { …}
func server() { …}
data Client Server
serverChan
freeList
![Page 13: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/13.jpg)
var freeList = make(chan *Buffer, 100)var serverChan = make(chan *Buffer)
func client() { for { b, ok := <-freeList // nonblocking if !ok { b = new(Buffer) } load(b) serverChan <- b }}
func server() { for { b := <-serverChan // blocking process(b) _ = freeList <- b // non-blocking }}
data Client Server
serverChan
freeListQ6
![Page 14: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency](https://reader036.fdocuments.in/reader036/viewer/2022070214/6113682591f9a204625543d9/html5/thumbnails/14.jpg)
EXERCISESSee GoConcurrency/parmap.go and parsort.go
Phot
o by
Ian
Sane
- h
ttp:
//flic
.kr/
p/86
XA
uo