-
Notifications
You must be signed in to change notification settings - Fork 86
/
rwmutex.go
77 lines (62 loc) · 1.22 KB
/
rwmutex.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
package main
import (
"fmt"
"sync"
"time"
)
type Bank struct {
sync.RWMutex
saving map[string]int // 每账户的存款金额
}
func NewBank() *Bank {
b := &Bank{
saving: make(map[string]int),
}
return b
}
// Deposit 存款
func (b *Bank) Deposit(name string, amount int) {
b.Lock()
defer b.Unlock()
if _, ok := b.saving[name]; !ok {
b.saving[name] = 0
}
b.saving[name] += amount
}
// Withdraw 取款,返回实际取到的金额
func (b *Bank) Withdraw(name string, amount int) int {
b.Lock()
defer b.Unlock()
if _, ok := b.saving[name]; !ok {
return 0
}
if b.saving[name] < amount {
amount = b.saving[name]
}
b.saving[name] -= amount
return amount
}
// Query 查询余额
func (b *Bank) Query(name string) int {
b.RLock()
defer b.RUnlock()
if _, ok := b.saving[name]; !ok {
return 0
}
return b.saving[name]
}
func main() {
b := NewBank()
go b.Deposit("xiaoming", 100)
go b.Withdraw("xiaoming", 20)
go b.Deposit("xiaogang", 2000)
time.Sleep(time.Second)
print := func(name string) {
fmt.Printf("%s has: %d\n", name, b.Query(name))
}
nameList := []string{"xiaoming", "xiaogang", "xiaohong", "xiaozhang"}
for _, name := range nameList {
go print(name)
}
time.Sleep(time.Second)
}