一、互斥锁
package main
import (
"fmt"
"sync"
"time"
"math/rand"
)
var (
//票数
num = 100
wg sync.WaitGroup
//互斥锁
mu sync.Mutex
)
func sellTicker(i int) {
defer wg.Done()
for {
//加锁,多个goroutine互斥
mu.Lock()
if num >= 1 {
fmt.Println("第", i, "个窗口卖了", num)
num = num - 1
}
//解锁
mu.Unlock()
if num <= 0 {
break
}
//添加休眠,防止结果可能出现在一个goroutine中
time.Sleep(time.Duration(rand.Int63n(1000) * 1e6))
}
}
func main() {
//设置随机数种子
rand.Seed(time.Now().UnixNano())
//计算器的起始值和票数相同
wg.Add(4)
go sellTicker(1)
go sellTicker(2)
go sellTicker(3)
go sellTicker(4)
wg.Wait()
fmt.Println("所有票卖完")
}
二、RWMutex读写锁
RWMutex 源码如下
// There is a modified copy of this file in runtime/rwmutex.go.
// If you make any changes here, see if you should make them there.
// A RWMutex is a reader/writer mutual exclusion lock.
// The lock can be held by an arbitrary number of readers or a single writer.
// The zero value for a RWMutex is an unlocked mutex.
//
// A RWMutex must not be copied after first use.
//
// If a goroutine holds a RWMutex for reading and another goroutine might
// call Lock, no goroutine should expect to be able to acquire a read lock
// until the initial read lock is released. In particular, this prohibits
// recursive read locking. This is to ensure that the lock eventually becomes
// available; a blocked Lock call excludes new readers from acquiring the
// lock.
type RWMutex struct {
w Mutex // held if there are pending writers
writerSem uint32 // semaphore for writers to wait for completing readers
readerSem uint32 // semaphore for readers to wait for completing writers
readerCount int32 // number of pending readers
readerWait int32 // number of departing readers
}
Go语言标准库中API如下
type RWMutex
func (rw *RWMutex) Lock()//禁止其他协程读写
func (rw *RWMutex) Unlock()
func (rw *RWMutex) RLock()//禁止其他协程写入,只能读取
func (rw *RWMutex) RUnlock()
func (rw *RWMutex) RLocker() Locke
package main
import (
"fmt"
"sync"
"strconv"
)
func main() {
var rwm sync.RWMutex
m := make(map[string]string)
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func(j int) {
//没有锁在map时可能出现问题
rwm.Lock()
m["key"+strconv.Itoa(j)] = "value" + strconv.Itoa(j)
fmt.Println(m)
rwm.Unlock()
wg.Done()
}(i)
}
wg.Wait()
fmt.Println("程序结束")
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。