1
2
3
4
5
6
7
8
9
10 package main
11
12 import (
13 "sync"
14 "sync/atomic"
15 )
16
17 var wg sync.WaitGroup
18
19 type S struct {
20 i1, i2 int32
21 }
22
23 var done int32
24
25 func (s S) Check(v1, v2 int32) {
26 for {
27 if g1 := atomic.LoadInt32(&s.i1); v1 != g1 {
28 panic(g1)
29 }
30 if g2 := atomic.LoadInt32(&s.i2); v2 != g2 {
31 panic(g2)
32 }
33 if atomic.LoadInt32(&done) != 0 {
34 break
35 }
36 }
37 wg.Done()
38 }
39
40 func F() {
41 s := S{1, 2}
42 go s.Check(1, 2)
43 atomic.StoreInt32(&s.i1, 3)
44 atomic.StoreInt32(&s.i2, 4)
45 atomic.StoreInt32(&done, 1)
46 }
47
48 func main() {
49 wg.Add(1)
50 F()
51 wg.Wait()
52 }
53
View as plain text