Skip to content

Latest commit

 

History

History
91 lines (65 loc) · 4.81 KB

README.md

File metadata and controls

91 lines (65 loc) · 4.81 KB

Goroutines

Goroutines are functions that are created and scheduled to be run independently by the Go scheduler. The Go scheduler is responsible for the management and execution of goroutines.

Notes

  • Goroutines are functions that are scheduled to run independently.
  • We must always maintain an account of running goroutines and shutdown cleanly. You can't kill a goroutine from outside.
  • Concurrency is not parallelism.
    • Concurrency is about dealing with lots of things at once.
    • Parallelism is about doing lots of things at once.

"Parallelism is TRYING to do two things at the same time. Concurrency is arranging it so you can do two things at the same time." - Dave Cheney

Design Guidelines

Concurrent Software Design

Concurrency means “out of order” execution. Taking a set of instructions that would otherwise be executed in sequence and finding a way to execute them out of order and still produce the same result. For the problem in front of you, it has to be obvious that out of order execution would add value. When I say value, I mean add enough of a performance gain for the complexity cost. Depending on your problem, out of order execution may not be possible or even make sense.

It’s also important to understand that concurrency is not the same as parallelism.

Parallelism means executing two or more instructions at the same time. This is a different concept from concurrency. Parallelism is only possible when you have at least 2 operating system (OS) and hardware threads available to you and you have at least 2 Goroutines, each executing instructions independently on each OS/hardware thread.

Both you and the runtime have a responsibility in managing the concurrency of the application. You are responsible for managing these three things when writing concurrent software:

Design Philosophy:

  • Keep integrity
  • Monitor critical points
  • Rate limit, set general limits
  • Use timeouts

Index of the three part series:

Diagrams

How the scheduler works.

Ardan Labs

Links

Code Review

Exercises

Exercise 1

Part A Create a program that declares two anonymous functions. One that counts down from 100 to 0 and one that counts up from 0 to 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.


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