当前位置:网站首页>【GoLang】同步锁 Mutex
【GoLang】同步锁 Mutex
2022-07-29 00:50:00 【JavaPub-rodert】
博主介绍: 自媒体 JavaPub 独立维护人,全网粉丝15w+,csdn博客专家、java领域优质创作者,51ctoTOP10博主,知乎/掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、
GoLang技术领域和副业。
同步锁
golang当中提供了两种常用的锁。
一种是 sync.Mutex (互斥锁) 另外一种是 sync.RWMutex (读写锁)
Mutex,它就是最简单最基础的同步锁,当一个goroutine持有锁的时候,其他的goroutine只能等待到锁释放之后才可以尝试持有。
RWMutex 是读写锁的意思,它支持一写多读,也就是说允许支持多个goroutine同时持有读锁,而只允许一个goroutine持有写锁。当有goroutine持有读锁的时候,会阻止写操作。当有goroutine持有写锁的时候,无论读写都会被堵塞。
案例
假设我们当前有多个goroutine,但是我们只希望持有锁的goroutine执行,我们可以这么写:
var lock sync.Mutex
for i := 0; i < 10; i++ {
go func() {
lock.Lock()
defer lock.Unlock()
// do something
}()
}
RWMutex区分了读写锁,所以我们一共会有4个api,分别是Lock, Unlock, RLock, RUnlock。Lock和Unlock是写锁的加锁以及解锁,而RLock和RUnlock自然就是读锁的加锁和解锁了。
面试题
Go 当中同步锁有什么特点?作用是什么?
- 阻塞。用于并发操作之间数据的安全保障。

边栏推荐
- Intel introduces you to visual recognition -- openvino
- [unity project practice] synthetic watermelon
- Cross modal alignment 20220728
- A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]
- [leetcode sliding window problem]
- T-sne dimensionality reduction
- Log4j dynamic loading configuration file
- Cloud native application comprehensive exercise
- HCIA配置实例(eNSP)
- JS judge whether array / object array 1 contains array / object array 2
猜你喜欢

Openpyxl merge cells

Platofarm community ecological gospel, users can get premium income with elephant swap
![[search] - DFS pruning and optimization](/img/d4/7c2fec02f5a6bcfa2d5e204398af01.png)
[search] - DFS pruning and optimization
![A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]](/img/aa/a5e7b4516aa395f8d4d0e2eee7d3c7.png)
A ten thousand word blog post takes you into the pit. Reptiles are a dead end [ten thousand word pictures]

【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域

【HCIP】MPLS 基础

What are source code, inverse code and complement code

C language 300 lines of code to achieve mine sweeping (deployable + markable + changeable difficulty level)

Self-attention neural architecture search for semantic image segmentation

Moonbeam上的多链用例解析——Derek在Polkadot Decoded 2022的演讲文字回顾
随机推荐
[unity project practice] synthetic watermelon
redis安装,集群部署与常见调优
ValueError: Colors must be aRGB hex values
After understanding the composition of the URL of the website, we use the URL module, querystring module and mime module to improve the static website
Canal实时解析mysql binlog数据实战
Flink Postgres CDC
[机缘参悟-54]:《素书》-1-事物缘起[原始章第一]:大道至简。
J9 number theory: what factors determine the value of NFT?
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
New upgrade: get Taobao product details "advanced version" API
Plato launched the LAAS protocol elephant swap, which allows users to earn premium income
els 新的方块落下
Behind the second round of okaleido tiger sales is the strategic support of ecological institutions
Flink SQL Hudi actual combat
HCIA configuration instance (ENSP)
Redis installation, cluster deployment and common tuning
internship:用于类型判断的工具类编写
Comprehensive upgrade, complete collection of Taobao / tmall API interfaces
Groundwater, soil, geology and environment
全新升级:获得淘宝商品详情“高级版” API
