gf/geg/os/gmlock/locker3.go

29 lines
548 B
Go

package main
import (
"sync"
"gitee.com/johng/gf/g/os/glog"
"time"
"gitee.com/johng/gf/g/os/gmlock"
)
// 内存锁 - TryLock
func main() {
key := "lock"
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
if gmlock.TryLock(key) {
glog.Println(i)
time.Sleep(time.Second)
gmlock.Unlock(key)
} else {
glog.Println(false)
}
wg.Done()
}(i)
}
wg.Wait()
}