golang 在使用读写锁时,切记出现读锁的嵌套,当程序在获取到第一个读锁的时候,此时执行了写锁,导致后续的读锁不能够获取,同样写锁也在等待第一个读锁的释放,从而造成死锁.

代码

 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
  package main

  import (
    "log"
    "net/http"
    _ "net/http/pprof"
    "sync"
    "time"
  )

  func main() {
    wg := sync.WaitGroup{}

    h := Hello{}
    wg.Add(1)
    go func() {
      defer wg.Done()
      t := time.NewTicker(time.Millisecond)
      for _ = range t.C {
        h.Get()
      }
    }()

    wg.Add(1)
    go func() {
      defer wg.Done()
      t := time.NewTicker(time.Second)
      for _ = range t.C {
        h.Set()
      }
    }()

    wg.Add(1)
    go func() {
      defer wg.Done()
      http.ListenAndServe(":8888", nil)
    }()

    wg.Wait()
  }

  type Hello struct {
    sync.RWMutex
  }

  func (h *Hello) Get() {
    h.RLock()
    defer h.RUnlock()

    // time.Sleep(time.Second)

    h.Query()
    log.Println("Get")
  }

  func (h *Hello) Query() {
    h.RLock()
    defer h.RUnlock()
    // time.Sleep(time.Second)
    log.Println("Query")
  }

  func (h *Hello) Set() {
    h.Lock()
    defer h.Unlock()
    // time.Sleep(time.Second)
    log.Println("Set")
  }

pprof

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
1 @ 0x42ec1c 0x42ed0e 0x43ff94 0x43fbb9 0x473679 0x6e1491 0x6e13e5 0x6e16ab 0x45d521
#	0x43fbb8	sync.runtime_Semacquire+0x38	/home/haormj/Project/go/src/runtime/sema.go:56
#	0x473678	sync.(*RWMutex).RLock+0x48	/home/haormj/Project/go/src/sync/rwmutex.go:50
#	0x6e1490	main.(*Hello).Query+0x30	/home/haormj/project/go/src/haormj.com/test/main.go:57
#	0x6e13e4	main.(*Hello).Get+0x64		/home/haormj/project/go/src/haormj.com/test/main.go:52
#	0x6e16aa	main.main.func1+0xaa		/home/haormj/project/go/src/haormj.com/test/main.go:20

1 @ 0x42ec1c 0x42ed0e 0x43ff94 0x43fbb9 0x47379e 0x6e1561 0x6e17ab 0x45d521
#	0x43fbb8	sync.runtime_Semacquire+0x38	/home/haormj/Project/go/src/runtime/sema.go:56
#	0x47379d	sync.(*RWMutex).Lock+0x6d	/home/haormj/Project/go/src/sync/rwmutex.go:98
#	0x6e1560	main.(*Hello).Set+0x30		/home/haormj/project/go/src/haormj.com/test/main.go:64
#	0x6e17aa	main.main.func2+0xaa		/home/haormj/project/go/src/haormj.com/test/main.go:29