+ All Categories
Home > Documents > EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman...

EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman...

Date post: 11-Mar-2021
Category:
Upload: others
View: 7 times
Download: 0 times
Share this document with a friend
14
EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency
Transcript
Page 1: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency

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

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

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

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

CONCURRENCY PATTERNS

Page 6: EMBEDDING AND CONCURRENCY · 2010. 11. 4. · EMBEDDING AND CONCURRENCY Curt Clifton Rose-Hulman Institute of Technology SVN Update, see GoConcurrency

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

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

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

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

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

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

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

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

EXERCISESSee GoConcurrency/parmap.go and parsort.go

Phot

o by

Ian

Sane

- h

ttp:

//flic

.kr/

p/86

XA

uo


Recommended