2017-11-23 10:21:28 +08:00
|
|
|
package main
|
2017-12-26 11:46:48 +08:00
|
|
|
|
2019-04-03 09:59:15 +08:00
|
|
|
import (
|
2019-06-18 08:37:21 +08:00
|
|
|
"github.com/gogf/gf/g/container/garray"
|
|
|
|
"github.com/gogf/gf/g/os/gmlock"
|
|
|
|
"github.com/gogf/gf/g/test/gtest"
|
|
|
|
"time"
|
2019-06-03 16:39:45 +08:00
|
|
|
)
|
2019-05-31 22:54:57 +08:00
|
|
|
|
2019-06-01 20:01:45 +08:00
|
|
|
func main() {
|
2019-06-18 08:37:21 +08:00
|
|
|
mu := gmlock.NewMutex()
|
|
|
|
array := garray.New()
|
|
|
|
go func() {
|
|
|
|
mu.LockFunc(func() {
|
|
|
|
array.Append(1)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
mu.LockFunc(func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
mu.LockFunc(func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(60 * time.Millisecond)
|
|
|
|
mu.Unlock()
|
|
|
|
mu.Unlock()
|
|
|
|
mu.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 1)
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 1)
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 3)
|
2019-06-12 21:06:57 +08:00
|
|
|
}
|