Skip to content

Commit b481519

Browse files
niaowaykevl
authored andcommitted
testdata, sync: add sync.Mutex test to testdata/coroutines.go
1 parent c54e1cc commit b481519

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

testdata/coroutines.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

3-
import "time"
3+
import (
4+
"sync"
5+
"time"
6+
)
47

58
func 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

5682
func sub() {

testdata/coroutines.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ async interface method call
1414
slept inside func pointer 8
1515
slept inside closure, with value: 20 8
1616
closure 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

0 commit comments

Comments
 (0)