...
1
2
3
4
5 package main
6
7 import "runtime"
8
9 func init() {
10 register("concurrentMapWrites", concurrentMapWrites)
11 register("concurrentMapReadWrite", concurrentMapReadWrite)
12 register("concurrentMapIterateWrite", concurrentMapIterateWrite)
13 }
14
15 func concurrentMapWrites() {
16 m := map[int]int{}
17 c := make(chan struct{})
18 go func() {
19 for i := 0; i < 10000; i++ {
20 m[5] = 0
21 runtime.Gosched()
22 }
23 c <- struct{}{}
24 }()
25 go func() {
26 for i := 0; i < 10000; i++ {
27 m[6] = 0
28 runtime.Gosched()
29 }
30 c <- struct{}{}
31 }()
32 <-c
33 <-c
34 }
35
36 func concurrentMapReadWrite() {
37 m := map[int]int{}
38 c := make(chan struct{})
39 go func() {
40 for i := 0; i < 10000; i++ {
41 m[5] = 0
42 runtime.Gosched()
43 }
44 c <- struct{}{}
45 }()
46 go func() {
47 for i := 0; i < 10000; i++ {
48 _ = m[6]
49 runtime.Gosched()
50 }
51 c <- struct{}{}
52 }()
53 <-c
54 <-c
55 }
56
57 func concurrentMapIterateWrite() {
58 m := map[int]int{}
59 c := make(chan struct{})
60 go func() {
61 for i := 0; i < 10000; i++ {
62 m[5] = 0
63 runtime.Gosched()
64 }
65 c <- struct{}{}
66 }()
67 go func() {
68 for i := 0; i < 10000; i++ {
69 for range m {
70 }
71 runtime.Gosched()
72 }
73 c <- struct{}{}
74 }()
75 <-c
76 <-c
77 }
78
View as plain text