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

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.

主站蜘蛛池模板: 柘城县| 和田市| 化德县| 裕民县| 古丈县| 藁城市| 水富县| 滦平县| 香河县| 渑池县| 孝感市| 惠水县| 叙永县| 新田县| 津市市| 浙江省| 大港区| 连南| 霸州市| 象州县| 乐清市| 平山县| 饶平县| 太湖县| 万载县| 故城县| 娄底市| 济宁市| 吉安市| 盖州市| 景东| 玉溪市| 怀远县| 富川| 九江县| 桂平市| 武汉市| 马关县| 南投市| 双城市| 宜兰市|