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.
How to create a mutex? Should we pass a copy of the mutex to the methods or the pointer?
m := &sync.mutex{}. Pass *m (as pointer).
What is the downside of using the “raw” sync.mutex? How to solve it?
Read operations should not block each other, only the write operation should block reads. Using RWMutex instead.
How to use a RWMutex?
RLock() to lock reads and Lock() to lock write operations.
When is best using RWMutex? Why not using it every time?
When read operations are much more often than write operations. It has some performance penalties when compared to a regular mutex.
What does the defer fmt.PrintLn(“Hi”) do?
Is executed after the nearest enclosing brackets is found.
What does Rob Pike says about mutexes and shared memory?
Don’t communicate by sharing memory, share memory by communicating.
What is the difference between unbuffered and buffered channels?
Unbuffered has to have a matching writer and receiver and unbuffered can define how many messages can exist without a receiver.
Is a channel bidirectional by default? Can I have send-only and receive-only channels?
Yes. Yes.
How to close a channel? What happens when an app tries to send data in the channel? What happens when it reads from a closed one?
Close(ch). The app panics. It reads the default value (0 for int).
Cite 1 usecase for closing a channel.
When you iterate over a channel (waiting for messages), e.g for msg := range ch {}. The loop will only exits when the channel is closed.