File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 11package main
22
3- import "time"
3+ import (
4+ "sync"
5+ "time"
6+ )
47
58func main () {
69 println ("main 1" )
@@ -51,6 +54,29 @@ func main() {
5154 println ("closure go call result:" , x )
5255
5356 time .Sleep (2 * time .Millisecond )
57+
58+ var m sync.Mutex
59+ m .Lock ()
60+ println ("pre-acquired mutex" )
61+ go acquire (& m )
62+ time .Sleep (2 * time .Millisecond )
63+ println ("releasing mutex" )
64+ m .Unlock ()
65+ time .Sleep (2 * time .Millisecond )
66+ m .Lock ()
67+ println ("re-acquired mutex" )
68+ m .Unlock ()
69+ println ("done" )
70+
71+ time .Sleep (2 * time .Millisecond )
72+ }
73+
74+ func acquire (m * sync.Mutex ) {
75+ m .Lock ()
76+ println ("acquired mutex from goroutine" )
77+ time .Sleep (2 * time .Millisecond )
78+ m .Unlock ()
79+ println ("released mutex from goroutine" )
5480}
5581
5682func sub () {
Original file line number Diff line number Diff line change @@ -14,3 +14,9 @@ async interface method call
1414slept inside func pointer 8
1515slept inside closure, with value: 20 8
1616closure go call result: 1
17+ pre-acquired mutex
18+ releasing mutex
19+ acquired mutex from goroutine
20+ released mutex from goroutine
21+ re-acquired mutex
22+ done
You can’t perform that action at this time.
0 commit comments