01-goroutines/

directory
v0.0.0-...-4ea1ca7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2015 License: Apache-2.0

README

Goroutines - Concurrency and Channels

Goroutines are functions that are created and scheduled to be run independently. Goroutines are multiplexed against a shared thread that is own by context. The scheduler is responsible for the management and execution of goroutines.

Notes

  • Goroutines are functions that are scheduled to run independently.
  • The scheduler uses a context that owns an OS thread and goroutine run queue.
  • We must always maintain an account of running goroutines and shutdown cleanly.
  • Concurrency is not parallelism.
    • Concurrency is about dealing with lots of things at once.
    • Parallelism is about doing lots of things at once.

Documentation

Scheduler Diagrams

Multi Processor and Thread Memory Access Issues

This content is provided by Scott Meyers from his talk in 2014 at Dive:

CPU Caches and Why You Care (32:46-35:40)

Notes

  • Cache lines (64 bytes) are moved in and out of the caches.
  • Two or more processors or cores may cache the same memory, cores need to know about activity.
  • Cache lines can be flagged as dirty and reloaded between the caches for a processor or core.
  • Leveraging the stack can give us exclusivity.
  • When a thread is migrated from one processor to another, all the cache lines have to be moved.
  • Swapping threads between processors can mean the swapping of cache.
  • Leveraging one thread against a consistent data set can provide better performance.
  • Hyper-Threading lets the processor work more than one thread at a time.
  • Even with HT, only one thread is executing at a time.

http://blog.golang.org/advanced-go-concurrency-patterns

http://blog.golang.org/context

http://blog.golang.org/concurrency-is-not-parallelism

http://talks.golang.org/2013/distsys.slide

http://www.goinggo.net/2014/01/concurrency-goroutines-and-gomaxprocs.html

http://www.akkadia.org/drepper/cpumemory.pdf

http://www.extremetech.com/extreme/188776-how-l1-and-l2-cpu-caches-work-and-why-theyre-an-essential-part-of-modern-chips

Code Review

Goroutines and concurrency (Go Playground)

Goroutine time slicing (Go Playground)

Goroutines and parallelism (Go Playground)

Exercises

Exercise 1

Part A Create a program that declares two anonymous functions. Once that counts up to 100 from 0 and one that counts down to 0 from 100. Display each number with an unique identifier for each goroutine. Then create goroutines from these functions and don't let main return until the goroutines complete.

Part B Run the program in parallel.

Template (Go Playground) | Answer (Go Playground)


Ardan Labs Ardan Studios GoingGo Blog


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show how to create goroutines and how the scheduler behaves.
Sample program to show how to create goroutines and how the scheduler behaves.
Sample program to show how the goroutine scheduler will time slice goroutines on a single thread.
Sample program to show how the goroutine scheduler will time slice goroutines on a single thread.
Sample program to show how to create goroutines and how the goroutine scheduler behaves with two contexts.
Sample program to show how to create goroutines and how the goroutine scheduler behaves with two contexts.
exercises
exercise1 command
Create a program that declares two anonymous functions.
Create a program that declares two anonymous functions.
template1 command
Create a program that declares two anonymous functions.
Create a program that declares two anonymous functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL