当前位置:网站首页>[golang] synchronization lock mutex
[golang] synchronization lock mutex
2022-07-29 01:51:00 【JavaPub-rodert】
Blogger introduction : We-Media JavaPub Independent maintainer , Fans all over the Internet 15w+,csdn Blogger 、java Quality creators in the field ,51ctoTOP10 Blogger , You know / Nuggets / Hua Wei Yun / Alibaba cloud /InfoQ And other high-quality authors 、 Focus on Java、
GoLangTechnical fields and sidelines .GoLang More convenient to learn , One article in hand , I have
GoLang interview | GoLang interview | GoLang interview
List of articles
Synchronization lock
golang Two commonly used locks are provided .
One is sync.Mutex ( The mutex ) The other is sync.RWMutex ( Read-write lock )
Mutex, It's the simplest and most basic synchronization lock , When one goroutine When holding a lock , Other goroutine You can only wait until the lock is released before trying to hold .
RWMutex It means read-write lock , It supports one write, many read , That is to say, it is allowed to support multiple goroutine Hold read lock at the same time , And only one is allowed goroutine Holding a write lock . When there is goroutine When holding a read lock , Will prevent write operations . When there is goroutine When holding a write lock , Both reading and writing will be blocked .
Case study
Suppose we have more than one goroutine, But we only want to hold the lock goroutine perform , We could write it this way :
var lock sync.Mutex
for i := 0; i < 10; i++ {
go func() {
lock.Lock()
defer lock.Unlock()
// do something
}()
}
RWMutex Distinguish between read and write locks , So we're going to have 4 individual api, Namely Lock, Unlock, RLock, RUnlock.Lock and Unlock It's the locking and unlocking of the write lock , and RLock and RUnlock Naturally, it is the locking and unlocking of read lock .
Interview questions
Go What are the characteristics of synchronous lock ? What is the role ?
- Blocking . Used for data security between concurrent operations .

边栏推荐
- [7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]
- 覆盖接入2w+交通监测设备,EMQ为深圳市打造交通全要素数字化新引擎
- Analyze OP based on autoware_ global_ Planner global path planning module re planning
- Tomorrow infinite plan, 2022 conceptual planning scheme for a company's yuanuniverse product launch
- [WesternCTF2018]shrine
- Reinforcement learning (III): dqn, nature dqn, double dqn, with source code interpretation
- Regular filtering data learning notes (①)
- Autoware reports an error: can't generate global path for start solution
- 承办首届算力大会,济南胜在何处?
- Practical experience of Google cloud spanner
猜你喜欢

Matplotlib Chinese question

BOM系列之window对象

10 major network security incidents in the past 10 years

SiC功率半导体产业高峰论坛成功举办

JVM learning minutes

Covering access to 2w+ traffic monitoring equipment, EMQ creates a new engine for the digitalization of all elements of traffic in Shenzhen

Read the recent trends of okaleido tiger and tap the value and potential behind it

【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索

Making high-precision map based on autoware (V)

Timer of BOM series
随机推荐
numpy.where() 用法和np.argsort()的用法
动态内存与智能指针
TypeError: can only concatenate str (not “int“) to str
The new generation of public chain attacks the "Impossible Triangle"
Tomorrow infinite plan, 2022 conceptual planning scheme for a company's yuanuniverse product launch
【GoLang】网络连接 net.Dial
New upgrade: get Taobao product details "advanced version" API
Slow storage scheme
10 major network security incidents in the past 10 years
Anaconda environment installation problem
StoneDB 邀请您参与开源社区月会!
About df['a column name'] [serial number]
【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
The brutal rule of blackmail software continues, and attacks increase by 105%
Code generator
【Unity项目实践】合成大西瓜
Timer of BOM series
Comprehensive upgrade, complete collection of Taobao / tmall API interfaces
numpy. Where() usage and np.argsort() usage
