fix issue in gmlock.Mutex.TryRLock

This commit is contained in:
John 2019-06-19 15:04:50 +08:00
parent dcf7138694
commit 405840607f
2 changed files with 33 additions and 21 deletions

View File

@ -7,9 +7,10 @@
package gmlock
import (
"github.com/gogf/gf/g/container/gtype"
"runtime"
"sync"
"github.com/gogf/gf/g/container/gtype"
)
// The high level RWMutex.
@ -111,6 +112,7 @@ func (m *Mutex) TryRLock() bool {
m.locking.Set(false)
return true
}
m.locking.Set(false)
}
return false
}

View File

@ -1,11 +1,12 @@
package main
import (
"fmt"
"time"
"github.com/gogf/gf/g/container/garray"
"github.com/gogf/gf/g/os/glog"
"github.com/gogf/gf/g/os/gmlock"
"github.com/gogf/gf/g/test/gtest"
"time"
)
func main() {
@ -14,30 +15,39 @@ func main() {
go func() {
mu.LockFunc(func() {
array.Append(1)
time.Sleep(10000 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
fmt.Println("====unlock")
})
}()
time.Sleep(10 * time.Millisecond)
for i := 0; i < 10000; i++ {
go func(i int) {
time.Sleep(50 * time.Millisecond)
mu.LockFunc(func() {
glog.Print(i)
array.Append(1)
})
}(i)
}
go func() {
time.Sleep(60 * time.Millisecond)
mu.Unlock()
mu.Unlock()
mu.Unlock()
time.Sleep(50 * time.Millisecond)
fmt.Println("tryRLock1")
mu.TryRLockFunc(func() {
array.Append(1)
fmt.Println("tryRLock1 success")
})
}()
go func() {
time.Sleep(150 * time.Millisecond)
fmt.Println("tryRLock2")
mu.TryRLockFunc(func() {
array.Append(1)
fmt.Println("tryRLock2 success")
})
}()
go func() {
time.Sleep(150 * time.Millisecond)
fmt.Println("tryRLock3")
mu.TryRLockFunc(func() {
array.Append(1)
fmt.Println("tryRLock3 success")
})
}()
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(), 1)
time.Sleep(150 * time.Millisecond)
fmt.Println("====array len:", array.Len())
gtest.Assert(array.Len(), 3)
}