mirror of
https://gitee.com/johng/gf.git
synced 2024-12-05 05:37:55 +08:00
29 lines
548 B
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()
|
||
|
}
|