sync 主要用于 golang 中共享资源访问的同步控制。虽然 golang 更推荐 chan 这种通过通信来共享资源的方式,但有时候直接使用 sync 的效果更好,它是 chan 方式的很好的补充。本文对 sync 的使用进行一些简单介绍。
互斥锁
func TestMutex() {
var mutex sync.Mutex
sum := 0
for i := 0; i < 10; i++ {
go func(t int) {
mutex.Lock()
defer mutex.Unlock()
sum += t
fmt.Println(t)
}(i)
}
time.Sleep(time.Second)
fmt.Printf("Sum: %v\n",sum)
}
条件变量
func TestCond() {
cond := sync.NewCond(&sync.Mutex{})
for i := 0; i < 10; i++ {
go func(t int) {
time.Sleep(time.Second)
cond.L.Lock()
defer cond.L.Unlock()
cond.Wait()
fmt.Println(t)
}(i)
}
time.Sleep(2 * time.Second)
//cond.Signal()
cond.Broadcast()
}
读写锁
func TestRWMutex() {
var rwMutex sync.RWMutex
sum := 0
for i := 0; i < 10; i++ {
go func(t int) {
rwMutex.RLock()
//time.Sleep(time.Second)
defer rwMutex.RUnlock()
fmt.Printf("Sum: %v\n",sum)
}(i)
go func(t int) {
rwMutex.Lock()
defer rwMutex.Unlock()
sum += t
}(i)
}
time.Sleep(5 * time.Second)
}
Pool
func TestPool() {
var pool sync.Pool
pool.New = func() interface{} { return "Hello" }
for i := 0; i < 10; i++ {
s := pool.Get()
fmt.Println(s)
pool.Put("World" + strconv.FormatInt(int64(i), 10))
}
}
Once
func TestOnce() {
once := &sync.Once{}
for i := 0; i < 10; i++ {
go func(t int) {
once.Do(func() {
fmt.Println("Hello Once!")
})
fmt.Println("Hello World!")
}(i)
}
time.Sleep(time.Second)
}
WaitGroup
func TestWaitGroup() {
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func(t int) {
defer wg.Done()
fmt.Println(t)
}(i)
}
wg.Wait()
}
原文链接:https://www.f2er.com/go/190090.html