File tree Expand file tree Collapse file tree 3 files changed +12
-22
lines changed
Expand file tree Collapse file tree 3 files changed +12
-22
lines changed Original file line number Diff line number Diff line change @@ -48,13 +48,13 @@ func MyFunc() {
4848例如这样:
4949
5050```
51- go build -tags ' deadlock' myproject
51+ go build -tags deadlock myproject
5252```
5353
5454同样这个标签也用于单元测试,否则默认的单元测试会死锁:
5555
5656```
57- go test -tags ' deadlock'
57+ go test -tags deadlock -v
5858```
5959
6060
Original file line number Diff line number Diff line change @@ -48,13 +48,13 @@ When you need to compile a deadlock detection enabled version. Just add `deadloc
4848For example:
4949
5050```
51- go build -tags ' deadlock' myproject
51+ go build -tags deadlock myproject
5252```
5353
5454This tag used for the unit test too. Otherwise the default unit test will deadlock:
5555
5656```
57- go test -tags ' deadlock'
57+ go test -tags deadlock -v
5858```
5959
6060How it works
Original file line number Diff line number Diff line change @@ -6,6 +6,14 @@ import (
66 "time"
77)
88
9+ func init () {
10+ go func () {
11+ for {
12+ time .Sleep (time .Second )
13+ }
14+ }()
15+ }
16+
917func Benchmark_Lock1 (b * testing.B ) {
1018 var mutex sync.Mutex
1119 for i := 0 ; i < b .N ; i ++ {
@@ -26,12 +34,6 @@ func Test_DeadLock1(t *testing.T) {
2634 var testDone WaitGroup
2735 testDone .Add (1 )
2836
29- go func () {
30- for {
31- time .Sleep (time .Second )
32- }
33- }()
34-
3537 var mutex1 Mutex
3638
3739 go func () {
@@ -50,12 +52,6 @@ func Test_DeadLock2(t *testing.T) {
5052 var testDone WaitGroup
5153 testDone .Add (1 )
5254
53- go func () {
54- for {
55- time .Sleep (time .Second )
56- }
57- }()
58-
5955 var (
6056 mutex1 Mutex
6157 mutex2 Mutex
@@ -87,12 +83,6 @@ func Test_DeadLock3(t *testing.T) {
8783 var testDone WaitGroup
8884 testDone .Add (1 )
8985
90- go func () {
91- for {
92- time .Sleep (time .Second )
93- }
94- }()
95-
9686 var (
9787 mutex1 Mutex
9888 mutex2 Mutex
You can’t perform that action at this time.
0 commit comments