Successfully reported this slideshow.
Your SlideShare is downloading. ×

Jaap Groeneveld - Concurrency in Go

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 17 Ad

More Related Content

Recently uploaded (20)

Advertisement

Jaap Groeneveld - Concurrency in Go

  1. 1. ConcurrencyinGo JaapGroeneveld-jGroeneveld.de
  2. 2. ConcurrencyvsParallelism ConcurrencyProgramming:Compositionofindependentlyexecutingprocesses ParallelProgramming:Simultaneousexecutionof(possiblyrelated) computations. . Concurrencyisaboutdealingwithlotsofthingsatonce. Parallelismisaboutdoinglotsofthingsatonce. Notthesame,butrelated. Concurrencyisaboutstructure,parallelismisaboutexecution. Concurrencyprovidesawaytostructureasolutiontosolveaproblemthatmay(but notnecessarily)beparallelizable. .RobPike:ConcurrencyvsParallelism 2
  3. 3. Goprovidesconcurrency Concurrentexecution(goroutines) Synchronizationandmessaging(channels) Multi-wayconcurrentcontrol(select) 3
  4. 4. Goroutines Agoroutineisafunctionrunningindependentlyinthesameaddressspaceasother goroutines foo("hello", "world") // foo runs; we wait go foo("hello", "world") // foo starts running bar() // does not wait for foo to return Goroutinesarenotthreads.It'sfinetostartthousandsofgoroutinesatthesametime.Not allwillruninparallelbutthisallowsforaconcurrentprogrammingmodel. 4
  5. 5. 5
  6. 6. Channels Channelsaretypedvaluesthatallowgoroutinestosynchronizeandexchangeinformation. ch := make(chan string) // Make Channel for strings close(ch) // Close Channel result := <- ch // Read (Blocking until result comes in) ch <- "hello" // Write (Blocking until read in case of buffer full or no buffer at all) Theycanbetypedtoonlybeusedforreading <-chan orwriting ->chan 6
  7. 7. Channelsexample timerChan := make(chan time.Time) go func() { time.Sleep(deltaT) // wait for a while timerChan <- time.Now() // send time on timerChan }() // [...] Do something else concurrently // Receive will block until timerChan delivers. completedAt := <-timerChan 7
  8. 8. PingPong 8
  9. 9. func pingpong() { var Ball int table := make(chan int) go player("1", table) go player("2", table) table <- Ball time.Sleep(1 * time.Second) <-table } func player(playerName string, table chan int) { for { ball := <-table ball++ time.Sleep(100 * time.Millisecond) table <- ball } } 9
  10. 10. Select Theselectstatementislikeaswitch,butthedecisionisbasedonabilitytocommunicate ratherthanequalvalues. Readfromoneofthechannelsifoneisreadyandthencontinue select { case v := <-ch1: fmt.Println("channel 1 sends", v) case v := <-ch2: fmt.Println("channel 2 sends", v) default: // optional fmt.Println("neither channel was ready") } Ifyouomitthedefaultcasethenthiswaitsuntilsomechannelssendsdata. 10
  11. 11. Mutex Protectsdataagainstconcurrentaccess. type Counter struct { mu sync.Mutex value int } func (c *Counter) Inc() { c.mu.Lock() defer c.mu.Unlock() c.value++ } ThereisalsoanRWMutextodifferentiatebetweenreadandwritelocking 11
  12. 12. Waitgroup Useawaitgrouptowaitforanumberofchannelsortaskstofinish var wg sync.WaitGroup wg.Add(numberOfTasks) for i:= 0; i < numberOfTasks; i++ { go func(w *sync.WaitGroup) { counter.Inc() w.Done() }(&wg) } wg.Wait() // block here until all are done 12
  13. 13. Guidelines Usechannelswhenpassingownershipofdata Usemutexesformanagingstate 13
  14. 14. Code Allexampleareincludedinthe src/basic folderwithadditionallogstatementsto understandthembetter. 14
  15. 15. Errors Toreceiveerrorsfromgoroutinesitsagreatwayto useerrorchannels make(chan err) ortointroduceastructthateitherincludesyourresultoranerror make(chan struct{Error error, Result MyResultType}) . Therearealsoconveniencelibrariesforthis.Forexample errgroup . See src/errors forexamples. 15
  16. 16. Patterns Fan-In (n producers -> 1 consumer) Fan-Out (1 producer -> n consumers) BackgroundWorker see src/patterns 16
  17. 17. https://yourbasic.org/golang/efficient-parallel-computation/ Morepatterns 17

×