当前位置:网站首页>Go language mutex lock
Go language mutex lock
2022-06-30 14:09:00 【Zhen Jie.】
sync.Mutex The mutex
type Mutex struct {
// Contains hidden or non exported fields
}
Mutex It's a mutex , Fields that can be created as other structures ; Zero is the unlocked state .Mutex Type of lock is thread independent , It can be locked and unlocked by different threads .
Lock
func (m *Mutex) Lock()
Lock The method locks m, If m Locked , Then block until m Unlock .
Unlock
func (m *Mutex) Unlock()
Unlock Method unlock m, If m Not locking will result in runtime errors . Locks have nothing to do with threads , It can be locked and unlocked by different threads .
Mutexes are mutually exclusive , Whoever grabs the lock will execute it .
scene :
var mutex sync.Mutex // The mutex
func printer(str string){
mutex.Lock() // Lock
defer mutex.Unlock() // Unlock
for _,ch:=range str{
fmt.Printf("%c",ch)
time.Sleep(time.Millisecond*300)
}
}
func user1(){
printer("hello ")
}
func user2(){
printer("world")
}
func main() {
go user1()
go user2()
for {
;
}
}
result : Every time 3 Millisecond output 1 Characters , Output... In sequence hello world 10 Characters 
Use steps :
1. var lock sync.Mutex // Declare a mutex
2.lock.Lock() // Lock
3.lock.Unlock() // Unlock
For more details, please refer to :http://blog.newbmiao.com/2019/10/10/go_mutex_insight.html
边栏推荐
- Step by step | help you easily submit Google play data security form
- Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
- Implementation of forwarding server using IO multiplexing
- Service online governance
- Je suis à Foshan, où puis - je ouvrir un compte? L'ouverture d'un compte par téléphone mobile est - elle sécurisée?
- 我如何才能保护我的私钥?
- 【刷题篇】供暖器
- Dart extended feature
- Wuenda 2022 machine learning special course evaluation is coming!
- Meaning of while (~scanf ("%d%d", & A, & B))
猜你喜欢

Assertions of regular series

Loss function: Diou loss handwriting implementation

正则系列之断言Assertions

【科学文献计量】外文文献及中文文献关键词的挖掘与可视化

Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone

MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video

"Persistent diseases" that cannot be solved in IM application development

Solve the error in my QT_ thread_ global_ End(): 3 threads didn't exit

“即服务”,企业数字化转型的必然选择

Defi "where does the money come from"? A problem that most people don't understand
随机推荐
可觀測,才可靠:雲上自動化運維CloudOps系列沙龍 第一彈
为基础性语言摇旗呐喊
知识传播不能取代专业学习!
golang模板(text/template)
想请教一下,我在佛山,到哪里开户比较好?手机开户是安全么?
About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)
I'd like to ask you, where can I open an account in Foshan? Is it safe to open a mobile account?
There is no utf8 option for creating tables in Navicat database.
get请求与post提交区别的简易理解
[deep anatomy of C language] storage principle of float variable in memory & comparison between pointer variable and "zero value"
mysql拒绝访问、管理员身份打开的
What is erdma as illustrated by Coptic cartoon?
Apache Doris Compaction优化百科全书
想請教一下,我在佛山,到哪裏開戶比較好?手機開戶是安全麼?
[the path of system analyst] Chapter 5 Software Engineering (Agile Development)
DeFi“钱从哪来”?一个大多数人都没搞清楚的问题
Service online governance
Assertions of regular series
IM即时通讯应用开发中无法解决的“顽疾”
Wuenda 2022 machine learning special course evaluation is coming!