A Tour Of Go - 线程

go 命令

线程共享内存, 需要 sync 包进行锁操作

mutex 锁

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
type Purse struct {
cash int
useLock bool
mux sync.Mutex
}

func (purse *Purse) Pay(price int) (int, bool) {
if purse.useLock {
purse.mux.Lock()
defer purse.mux.Unlock()
}

if purse.cash >= price {
purse.cash -= price
return purse.cash, true
} else {
return purse.cash, false
}
}

purse := &Purse{cash: 1010, useLock: false}
for t := 0; t < 15; t++ {
go func() {
fmt.Println(purse.Pay(100))
}()
}

time.Sleep(time.Second)

channel 信道

初始化 ch := make(chan int, bufLen)
发送 ch <- v
接收 v, ok := <-ch
关闭 close(ch)

信道在双方开启前处于阻塞状态, 方便线程之间进行数据同步, 而无需加锁.

信道满后, 写操作阻塞; 信道空后, 读操作阻塞

ok 为 false 表明信道无数据并且已关闭

只有发送者可以关闭信道, 发送者不可以; 信道关闭后再发送数据将触发 panic

一般情况无需关闭信道, 除非接受者必须接收到关闭后触发某些操作.

select

for {
select {
case c <- x:

case <- quit:

    return
default:
    // 无阻塞, 循环非常快
}

}

定时

interval time.Tick(100 * time.Millisecond)
timeout time.After(500 * time.Millisecond)
sleep time.Sleep(10 * time.Millisecond)

练习

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
func Walk(t *tree.Tree, ch chan int) {
if t.Left != nil {
Walk(t.Left, ch)
}
ch <- t.Value
if t.Right != nil {
Walk(t.Right, ch)
}
}

func Same(t1, t2 *tree.Tree) bool {
var v1, v2 int
ch1 := make(chan int, 1)
ch2 := make(chan int, 1)
go Walk(t1, ch1)
go Walk(t2, ch2)
for idx := 0; idx < 10; idx++ {
v1 = <-ch1
v2 = <-ch2
if v1 != v2 {
return false
}
}
return true
}

func Test1() {
ch := make(chan int, 1)
go Walk(tree.New(1), ch)
for idx := 0; idx < 5; idx++ {
v, ok := <-ch
fmt.Println(v, ok)
}
}

func Test2() {
var same bool
same = Same(tree.New(1), tree.New(1))
fmt.Printf("Tree same ? %t\n", same)

same = Same(tree.New(1), tree.New(2))
fmt.Printf("Tree same ? %t\n", same)
}
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.