Concurrent Programming with Go Flashcards
What is the difference between concurrency and multi tasking?
Concurrency is having multiple tasks but executing a piece of each in a single thread and parallelism is to execute multiple tasks simultaneously.
How do we call a []int?
Slice of int.
What does this code do?
if b, ok := methd(); ok {fmt.println(“ok”)}
Will print ok if ok is true.
Do threads and goroutine have its own stack? How big is the stack in each case?
Yes. OS has ~1MB and goroutine starts at 2KB.
Can the goroutine stack grow?
Yes.
Who manages the threads and the goroutines?
OS and go runtime respectively.
How to create an anonymous function in go?
var abc := 1; func (param int) { }
Should we share values from the main thread stack with goroutines? How to pass then?
No. Via params.
What happens if your main function calls a go routine and do not wait for it?
It finishes (and the app exits) prematurely and the goroutine won’t finish in time.
What are the two main challenges of concurrency?
Coordinate tasks and share memory between the tasks and main thread.
What does the sync.WaitGroup do?
Waits for a collection of goroutines to finish.
What does a mutex do?
Ensures that a shared variable is accessed only once even when multiple goroutines try to access it at the same time
What does the “–race” in go run –race main.go do?
It adds instrumentation to the executable to find and report racing conditions.
What is the warning generated by a racing condition when using –race flag?
WARNING: DATA RACE
Is the DATA RACE warning only generated when something fails?
No, it reports the racing condition even when the application does not panic.