官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 白城市| 舟山市| 田阳县| 双城市| 合山市| 洛隆县| 抚松县| 东辽县| 孝昌县| 辽中县| 江达县| 秦皇岛市| 南投县| 资中县| 峡江县| 古丈县| 丰宁| 文昌市| 衢州市| 普安县| 韩城市| 阿荣旗| 淮北市| 修水县| 高尔夫| 苗栗县| 贡嘎县| 马边| 清苑县| 尚义县| 正宁县| 凯里市| 沐川县| 马关县| 定安县| 遂昌县| 太原市| 蒙阴县| 全州县| 仙居县| 中牟县|