-
Notifications
You must be signed in to change notification settings - Fork 127
/
lesson4.slide
89 lines (69 loc) · 2.39 KB
/
lesson4.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Concurrency
Lesson 4
30 May 2024
Tags: golang, go
Stanislav Kozina <skozina@redhat.com>
Red Hat, Inc.
https://github.com/RedHatOfficial/GoCourse
* Sources
- [[https://github.com/RedHatOfficial/GoCourse]]
.image ./common/qr_address.png
* Concurrency
- Definition: Computations happening at the same time
- Even on single CPU (kernel makes that feeling)
* Communication via message queues
- Analogy of phone calls
- Phone ringing: blocking call
- Answerphone: non-blocking call (tape length = queue size)
- Multiple receivers: Call centre
- Multiple senders: Conf call
* Threads and goroutines
- Reminder: Threads share memory
- Go routines are light-weighted threads, cheap to spawn
- It should be fine to spawn thousands of goroutines
- Internally multiplexed across thread pool
- Spawned with simple go keyword
* Goroutines
.play lesson4/goroutines.go
* Channels
- Way to transfer data between goroutines
- Type of data is part of the channel type
- By default blocks on both tx/rx (but can be buffered too)
- Same operator to send and receive from channel
- Created with make()
.code lesson4/chan.go
* Channels
- Note that this is racy.. more about that later
.play lesson4/chan2.go
* Channels
- Unbuffered channels do block (and golang kindly detects deadlock)
.play lesson4/chan3.go
* Buffered channels
- Size of buffer as second argument to make()
.play lesson4/chan4.go
* Closing channels
- Channels can be closed with close()
- Readers can check for closed channels using optional second return argument
- Alternatively readers can detect closed channel with the loop over range
* Closed channel detection with return value
.play lesson4/chan_close.go
* Closed channel detection with range
.play lesson4/range.go
* Select on multiple channels
- You can read wait on multiple channels at the same time using select
* Select on multiple channels
.play lesson4/select.go
* Quit + ack pattern
.play lesson4/quit_ack.go /^package main/,/^func main/
* Quit + ack pattern (cont.)
.play lesson4/quit_ack.go /^func main/,/^}/
* Default selection
- Default action to take if all other actions would block
* Default selection
.play lesson4/default.go
#last slide
* More Gophers
#The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
#Source https://golang.org/doc/gopher/bumper.png
#The design and this image is licensed under the Creative Commons 3.0 Attributions license.
.image ./common/bumper.png _ 900