- Hands-On Full Stack Development with Go
- Mina Andrawos
- 94字
- 2021-07-02 12:33:34
The simple mutex
In Go, a simple lock is a pointer to a mutex struct type, which belongs to the sync package. We can create a mutex as follows:
var myMutex = &sync.Mutex{}
Let's assume that we have a map called myMap of the map[int]int type that we'd like to protect from the concurrent access of multiple goroutines:
myMutex.Lock()
myMap[1] = 100
myMutex.Unlock()
If we ensure that all goroutines that need to edit myMap have access to myMutex, we can protect myMap against multiple goroutines changing it at the same time.
推薦閱讀
- Design Principles for Process:driven Architectures Using Oracle BPM and SOA Suite 12c
- Mastering SVG
- C語(yǔ)言程序設(shè)計(jì)基礎(chǔ)與實(shí)驗(yàn)指導(dǎo)
- 程序員考試案例梳理、真題透解與強(qiáng)化訓(xùn)練
- 精通Python設(shè)計(jì)模式(第2版)
- 深入淺出Serverless:技術(shù)原理與應(yīng)用實(shí)踐
- ASP.NET Core 2 Fundamentals
- RESTful Java Web Services(Second Edition)
- 深入實(shí)踐Kotlin元編程
- Android應(yīng)用開(kāi)發(fā)深入學(xué)習(xí)實(shí)錄
- UX Design for Mobile
- Python數(shù)據(jù)科學(xué)實(shí)踐指南
- iOS Development with Xamarin Cookbook
- 用Python動(dòng)手學(xué)統(tǒng)計(jì)學(xué)
- Android 5從入門(mén)到精通