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

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.

主站蜘蛛池模板: 驻马店市| 寿阳县| 林州市| 宜丰县| 奇台县| 朝阳区| 古田县| 梧州市| 宜君县| 嵊泗县| 南通市| 临清市| 浏阳市| 宜州市| 平乐县| 玉树县| 微博| 丹江口市| 徐州市| 虎林市| 永福县| 阿城市| 神池县| 布尔津县| 定西市| 安乡县| 泰兴市| 杂多县| 凤凰县| 溧水县| 名山县| 满城县| 胶南市| 文安县| 东乡族自治县| 富平县| 颍上县| 南充市| 襄汾县| 美姑县| 庄浪县|