当前位置:网站首页>GO语言-锁操作
GO语言-锁操作
2022-06-25 15:39:00 【一边学习一边哭】
概述
sync包文档:Package sync - The Go Programming Language
sync包提供了基本同步的操作,如互斥锁等。除了Once和 WaitGroup之外,大多数类型都是提供低级库例程使用的。而更高级的的同步最好是通过channel和通信来完成。
互斥锁 Mutex
互斥锁比较暴力,当一个goroutine获得了互斥锁之后,其他的goroutine只能等待释放锁。
使用互斥锁时,对资源操作完一定要解锁,否则程序会死锁。建议使用defer语句来解锁。
//定义互斥锁对象
var mutex_name sync.Mutex
//上锁
mutex_name.Lock()
//解锁
mutex_name.Unlock()同样是的火车站售票问题,我们看一下通过互斥锁解决临界资源问题的效果
//车票余量
var tickets = 5
//定义同步等待组
var wg sync.WaitGroup
//定义互斥锁对象
var mutex sync.Mutex
func main() {
wg.Add(3)
go saleTickets("1号售票口")
go saleTickets("2号售票口")
go saleTickets("3号售票口")
wg.Wait()
}
func saleTickets(name string) {
for {
mutex.Lock()
if tickets > 0 {
fmt.Printf("车票剩余:%v, %v售出\n", tickets, name)
tickets--
time.Sleep(1 * time.Second)
} else {
mutex.Unlock()
fmt.Println("车票已售罄...")
wg.Done()
break
}
mutex.Unlock()
}
}
读写锁 RWMutex
读写锁和普通的互斥锁不同的是,它是一种专门针对读操作和写操作的互斥锁。
读写锁同一时刻允许有多个读操作在进行,但在同一时刻只允许有一个写操作在进行。并且在某一个写操作进行过程中,读操作也是不可以进行的。所以说,读写锁对于读操作可以同时并发,但是对于写操作是完全互斥的。
//定义读写锁锁对象
var rwmutex_name sync.RWMutex
//读锁
rwmutex_name.RLock
//读解锁
rwmutex_name.RUnlock
//写锁
rwmutex_name.Lock
//写解锁
rwmutex_name.Unlock读写锁我们就不进行示例的展示了,因为和上面互斥锁展示的示例效果应该差别不大。
对写锁实际解决的问题是,使用互斥锁带来并发能力降低的问题。因为互斥锁不论读操作还是写操作都会阻塞,程序从并发变成了串行。
而实际上对于不会对临界资源产生修改的读操作,我们是不需要阻塞的;只有对临界资源需要修改的写操作,我们才需要阻塞。所以读写锁就是为了提高程序加锁时的并发能力。
边栏推荐
- The style of the mall can also change a lot. DIY can learn about it!
- 异步处理容易出错的点
- js 给元素添加自定义属性
- Mt60b1g16hc-48b:a micron memory particles FBGA code d8bnk[easy to understand]
- Constructor Pattern
- Golang open source streaming media audio and video network transmission service -lal
- Uniapp converts graphic verification codes in the form of file streams into images
- Lombok common notes
- [golang] leetcode intermediate - find the first and last position of an element in a sorted array & Merge interval
- 一行代码可以做什么?
猜你喜欢

合宙Air32F103CBT6開發板上手報告

Vscode有什么好用的插件?

Download and installation tutorial of consumer

Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down

Optimization of lazyagg query rewriting in parsing data warehouse

商城风格也可以很多变,DIY了解一下!

Several relationships of UML

Sword finger offer 03 Duplicate number in array
Inter thread synchronization semaphore control

面试官:你简历上说精通mysql,那你说下聚簇/联合/覆盖索引、回表、索引下推
随机推荐
镁光256Gb NAND Flash芯片介绍
Built in methods for data types
什么是oa
Binocular 3D perception (I): preliminary understanding of binocular
Mixed density network (MDN) for multiple regression explanation and code example
leetcode-8. String to integer (ATOI)
golang使用mongo-driver操作——增(进阶)
Traversal and branch judgment of JS (case on June 24, 2022)
Don't underestimate the integral mall, its role can be great!
GridLayout evenly allocate space
加载本地cifar10 数据集
js 给元素添加自定义属性
leetcode-8. 字符串转换整数 (atoi)
Do you want to go to an outsourcing company? This article will give you a comprehensive understanding of outsourcing pits!
说下你对方法区演变过程和内部结构的理解
Load local cifar10 dataset
Prototype mode
Report on Hezhou air32f103cbt6 development board
The style of the mall can also change a lot. DIY can learn about it!
Golang uses Mongo driver operation - increase (Advanced)