-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrency.slide
89 lines (44 loc) · 2.85 KB
/
concurrency.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
Concurrency
Brian Ketelsen
@bketelsen
* Concurrency
* Goroutines
A goroutine is a function that is capable of running concurrently with other functions.
Goroutines aren't threads. They're smaller, lighter, and less resource intensive. A goroutine only requires a few bytes of memory, compared to a thread. Each thread has a fixed stack size, so there's a finite limit to the number of threads you can run. In contrast, it's common to run millions of goroutines at once.
* Goroutines
To create a goroutine, simply prepend the "go" keyword before a function call.
- Create Goroutine
cd $GOPATH/src/github.com/gophertrain/material/concurrency/demos/goroutine/
go run main.go
Why didn't that work?
* Goroutines
Goroutines execute concurrently. In this case, main() and sayHello() were running at the same time. main() finished and sayHello() never got a chance to run!
To solve this problem you need to use some sort of communication or synchronization device to signal that a goroutine is complete.
We typically do that with channels or waitgroups, depending on whether you need to return a value from the function you're running.
* Waitgroups
WaitGroups are defined in the "sync" package in the standard library. They're a simple and powerful way to ensure that all of the goroutines you launch have completed before you move on to do other things.
- Waitgroup Example
cd $GOPATH/src/github.com/gophertrain/material/concurrency/demos/waitgroup/
go run main.go
WaitGroups are also especially powerful when combined with closures (anonymous functions)
.link https://golang.org/pkg/sync/#example_WaitGroup Closure Example
* Channels
Channels are a typed conduit through which you can send and receive values.
.link https://tour.golang.org/concurrency/2 Channel Overview
* Channels
By default channels are unbuffered -- sends on a channel will block until there is something ready to receive from it.
You can create a buffered channel- which allows you to control how many items can sit in a channel unread.
.link https://tour.golang.org/concurrency/3 Buffered Channels
* Channels
You can signal that there are no more values in a channel by closing it. Receivers can test to see if a channel is closed during a read.
.link https://tour.golang.org/concurrency/4 Closing Channels
* Channels
You can use a select statement to allow a goroutine to operate on multiple communication operations:
.link https://tour.golang.org/concurrency/5 Select
* Blocking Channels
If no operations inside a select statement can process, the select will block. To work around this problem, you can add a default statement to your select:
.link https://tour.golang.org/concurrency/6 Select With Default
* Exercise
Read Dave Cheney's article on some of the more interesting properties of channels.
.link https://dave.cheney.net/2013/04/30/curious-channels Curious Channels