Files
gf/.example/os/gmutex/gmutex_basic.go

30 lines
456 B
Go
Raw Normal View History

2019-07-13 16:41:23 +08:00
package main
import (
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/os/gmutex"
2019-07-13 16:41:23 +08:00
)
func main() {
mu := gmutex.New()
for i := 0; i < 10; i++ {
go func(n int) {
mu.Lock()
defer mu.Unlock()
glog.Println("Lock:", n)
time.Sleep(time.Second)
}(i)
}
for i := 0; i < 10; i++ {
go func(n int) {
mu.RLock()
defer mu.RUnlock()
glog.Println("RLock:", n)
time.Sleep(time.Second)
}(i)
}
time.Sleep(11 * time.Second)
}