gf/.example/os/gmlock/locker4.go

40 lines
773 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"time"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/os/gmlock"
)
// 内存锁 - 手动Unlock与计时Unlock冲突校验
func main() {
key := "key"
// 第一次锁带时间
gmlock.Lock(key, 1000)
glog.Println("lock1")
// 这个时候上一次的计时解锁已失效
gmlock.Unlock(key)
glog.Println("unlock1")
fmt.Println()
// 第二次锁不带时间且在执行过程中钱一个Lock的定时解锁生效
gmlock.Lock(key)
glog.Println("lock2")
go func() {
// 正常情况下3秒后才能执行这句
gmlock.Lock(key)
glog.Println("lock by goroutine")
}()
time.Sleep(3 * time.Second)
// 这时再解锁
gmlock.Unlock(key)
// 注意3秒之后才会执行这一句
glog.Println("unlock2")
select {}
}