- 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.
推薦閱讀
- 計算思維與算法入門
- SoapUI Cookbook
- 深度學(xué)習(xí)經(jīng)典案例解析:基于MATLAB
- Vue.js 3.0源碼解析(微課視頻版)
- Apache Spark 2.x for Java Developers
- Kotlin從基礎(chǔ)到實戰(zhàn)
- 精通MATLAB(第3版)
- Red Hat Enterprise Linux Troubleshooting Guide
- Access 2010數(shù)據(jù)庫應(yīng)用技術(shù)實驗指導(dǎo)與習(xí)題選解(第2版)
- 實戰(zhàn)Java高并發(fā)程序設(shè)計(第2版)
- Natural Language Processing with Python Quick Start Guide
- Windows Phone 8 Game Development
- Practical GIS
- 深入淺出 HTTPS:從原理到實戰(zhàn)
- 軟件開發(fā)中的決策:權(quán)衡與取舍