-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (88 loc) · 1.52 KB
/
main.go
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
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"log"
"sync"
"sync/atomic"
"time"
)
type Wb struct {
Obj *int
}
var A Wb
var B Wb
func simpleSet(c *int) {
A.Obj = nil
B.Obj = c
A.Obj = c
B.Obj = nil
fmt.Println("A:%d,b:%d", A, B)
}
type Value struct {
Name string
GoodAt []string
map1 map[string]string
}
/*func main() {
ctx, cancel := context.WithCancel(context.Background())
num := 0
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("goroutine exit")
return
case <-time.After(time.Second):
num++
fmt.Printf("goroutine wait times: %d\n", num)
}
}
}(ctx)
time.Sleep(time.Second * 4)
cancel()
fmt.Println("main exit")
fmt.Printf("goroutine wait times: %d\n", num)
}
*/
var (
counter int32 //计数器
wg sync.WaitGroup //信号量
)
var ch chan struct{} = make(chan struct{}, 2)
func foo() {
ch <- struct{}{}
log.Println("foo() 000")
ch <- struct{}{}
log.Println("foo() 111")
time.Sleep(5 * time.Second)
log.Println("foo() 222")
close(ch)
log.Println("foo() 333")
}
func main() {
var b struct{}
log.Println("main() 111")
go foo()
log.Println("main() 222")
a := <-ch
log.Println("main() 333", a)
b = <-ch
log.Println("main() 444", b)
c := <-ch
log.Println("main() 555", c)
}
func incCounter(index int) {
defer wg.Done()
spinNum := 0
for {
//2.1原子操作
old := counter
ok := atomic.CompareAndSwapInt32(&counter, old, old+1)
if ok {
break
} else {
spinNum++
}
}
fmt.Printf("thread,%d,spinnum,%d\n", index, spinNum)
}