Concurrent Programming with Go Flashcards

1
Q

What is the difference between concurrency and multi tasking?

A

Concurrency is having multiple tasks but executing a piece of each in a single thread and parallelism is to execute multiple tasks simultaneously.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do we call a []int?

A

Slice of int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does this code do?

if b, ok := methd(); ok {fmt.println(“ok”)}

A

Will print ok if ok is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Do threads and goroutine have its own stack? How big is the stack in each case?

A

Yes. OS has ~1MB and goroutine starts at 2KB.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can the goroutine stack grow?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Who manages the threads and the goroutines?

A

OS and go runtime respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to create an anonymous function in go?

A
var abc := 1;
func (param int) {
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Should we share values from the main thread stack with goroutines? How to pass then?

A

No. Via params.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if your main function calls a go routine and do not wait for it?

A

It finishes (and the app exits) prematurely and the goroutine won’t finish in time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the two main challenges of concurrency?

A

Coordinate tasks and share memory between the tasks and main thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the sync.WaitGroup do?

A

Waits for a collection of goroutines to finish.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does a mutex do?

A

Ensures that a shared variable is accessed only once even when multiple goroutines try to access it at the same time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the “–race” in go run –race main.go do?

A

It adds instrumentation to the executable to find and report racing conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the warning generated by a racing condition when using –race flag?

A

WARNING: DATA RACE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Is the DATA RACE warning only generated when something fails?

A

No, it reports the racing condition even when the application does not panic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to create a mutex? Should we pass a copy of the mutex to the methods or the pointer?

A

m := &sync.mutex{}. Pass *m (as pointer).

17
Q

What is the downside of using the “raw” sync.mutex? How to solve it?

A

Read operations should not block each other, only the write operation should block reads. Using RWMutex instead.

18
Q

How to use a RWMutex?

A

RLock() to lock reads and Lock() to lock write operations.

19
Q

When is best using RWMutex? Why not using it every time?

A

When read operations are much more often than write operations. It has some performance penalties when compared to a regular mutex.

20
Q

What does the defer fmt.PrintLn(“Hi”) do?

A

Is executed after the nearest enclosing brackets is found.

21
Q

What does Rob Pike says about mutexes and shared memory?

A

Don’t communicate by sharing memory, share memory by communicating.

22
Q

What is the difference between unbuffered and buffered channels?

A

Unbuffered has to have a matching writer and receiver and unbuffered can define how many messages can exist without a receiver.

23
Q

Is a channel bidirectional by default? Can I have send-only and receive-only channels?

A

Yes. Yes.

24
Q

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?

A

Close(ch). The app panics. It reads the default value (0 for int).

25
Q

Cite 1 usecase for closing a channel.

A

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.