File tree 4 files changed +43
-6
lines changed
4 files changed +43
-6
lines changed Original file line number Diff line number Diff line change @@ -281,6 +281,7 @@ go tool objdump -s main.main exec
281
281
282
282
- [ Race Example] ( https://github.com/golang-basics/concurrency/blob/master/atomics/race/main.go )
283
283
- [ Basic Counter] ( https://github.com/golang-basics/concurrency/blob/master/atomics/basic-counter/main.go )
284
+ - [ Race Fixed Example] ( https://github.com/golang-basics/concurrency/blob/master/atomics/race-fixed/main.go )
284
285
- [ atomic.Value - Reader/Writer Example] ( https://github.com/golang-basics/concurrency/blob/master/atomics/value/reader-writer/main.go )
285
286
- [ atomic.Value - Calculator Example] ( https://github.com/golang-basics/concurrency/blob/master/atomics/value/calculator/main.go )
286
287
- [ atomic.Value - panic] ( https://github.com/golang-basics/concurrency/blob/master/atomics/value/panic/main.go )
Original file line number Diff line number Diff line change @@ -15,19 +15,19 @@ func main() {
15
15
16
16
wg .Add (1 )
17
17
go func () {
18
+ defer wg .Done ()
18
19
time .Sleep (10 * time .Millisecond )
19
- fmt .Println (atomic .LoadInt64 (& count ))
20
- wg .Done ()
20
+ fmt .Println ("count in go routine" , atomic .LoadInt64 (& count ))
21
21
}()
22
22
23
+ wg .Add (50 )
23
24
for i := 0 ; i < 50 ; i ++ {
24
- wg .Add (1 )
25
25
go func () {
26
+ defer wg .Done ()
26
27
time .Sleep (10 * time .Millisecond )
27
28
atomic .AddInt64 (& count , 1 )
28
- wg .Done ()
29
29
}()
30
30
}
31
31
wg .Wait ()
32
- fmt .Println (count )
32
+ fmt .Println ("count in main" , count )
33
33
}
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "sync"
6
+ "sync/atomic"
7
+ )
8
+
9
+ func main () {
10
+ var count int32
11
+ var wg sync.WaitGroup
12
+ wg .Add (5 )
13
+ go func () {
14
+ defer wg .Done ()
15
+ atomic .StoreInt32 (& count , 10 )
16
+ }()
17
+ go func () {
18
+ defer wg .Done ()
19
+ atomic .StoreInt32 (& count , - 15 )
20
+ }()
21
+ go func () {
22
+ defer wg .Done ()
23
+ atomic .StoreInt32 (& count , 1 )
24
+ }()
25
+ go func () {
26
+ defer wg .Done ()
27
+ atomic .StoreInt32 (& count , 0 )
28
+ }()
29
+ go func () {
30
+ defer wg .Done ()
31
+ atomic .StoreInt32 (& count , 100 )
32
+ }()
33
+ wg .Wait ()
34
+
35
+ fmt .Println ("count" , count )
36
+ }
Original file line number Diff line number Diff line change @@ -35,14 +35,14 @@ func main() {
35
35
wg .Add (5 )
36
36
for i := 0 ; i < 5 ; i ++ {
37
37
go func () {
38
+ defer wg .Done ()
38
39
// we're gonna get a panic this way
39
40
// cfg := v.Load().(Config)
40
41
cfg , ok := v .Load ().(Config )
41
42
if ! ok {
42
43
log .Fatalf ("received different type: %T" , cfg )
43
44
}
44
45
fmt .Println ("cfg" , cfg )
45
- wg .Done ()
46
46
}()
47
47
}
48
48
wg .Wait ()
You can’t perform that action at this time.
0 commit comments