当前位置:网站首页>Concurrent writing of maps in golang
Concurrent writing of maps in golang
2022-06-24 14:33:00 【KunkkaWu】
principle
golang Medium map Not thread safe , Therefore, it cannot be used directly in the case of concurrency map.
Negative example
import (
"strconv"
"time"
)
var m = make(map[string]interface{})
func main() {
testMap(m)
time.Sleep(2 * time.Second)
}
func testMap() {
go func() {
for i := 0; i < 10; i++ {
name := "WangWu" + strconv.Itoa(i)
writeMap(m, name, i)
}
}()
go func() {
for i := 0; i < 10; i++ {
name := "ZhuoQi" + strconv.Itoa(i)
writeMap(m, name, i)
}
}()
}
func writeMap(m map[string]int, key string, val int) {
m[key] = val
}Runtime , Will report a mistake :
fatal error: concurrent map writesterms of settlement
1. Map Lock
package main
import (
"fmt"
"strconv"
"sync"
"time"
)
// Definite lock
var locker = &sync.RWMutex{}
var m = make(map[string]interface{})
func main() {
testMap()
time.Sleep(2 * time.Second)
fmt.Println(m)
}
func testMap() {
go func() {
for i := 0; i < 10; i++ {
name := "WangWu" + strconv.Itoa(i)
writeMap(name, i)
}
}()
go func() {
for i := 0; i < 10; i++ {
name := "ZhuoQi" + strconv.Itoa(i)
writeMap(name, i)
}
}()
}
func writeMap(key string, val int) {
locker.Lock()
m[key] = val
locker.Unlock()
}- Use sync.Map
package main
import (
"fmt"
"strconv"
"sync"
"time"
)
var m = sync.Map{}
func main() {
testMap()
time.Sleep(2 * time.Second)
m.Range(func(k, v interface{}) bool {
fmt.Println("map:", k, v)
return true
})
}
func testMap() {
go func() {
for i := 0; i < 10; i++ {
name := "WangWu" + strconv.Itoa(i)
m.Store(name, i)
}
}()
go func() {
for i := 0; i < 10; i++ {
name := "ZhuoQi" + strconv.Itoa(i)
m.Store(name, i)
}
}()
}边栏推荐
- Mots clés pour la cartographie es; Ajouter une requête par mot - clé à la requête term; Changer le type de mot - clé de cartographie
- ES mapping之keyword;term查詢添加keyword查詢;更改mapping keyword類型
- 【Pytorch】量化
- 成功解决:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This versi
- 阿里OSS对象存储服务
- 六月集训(第24天) —— 线段树
- MES在流程和离散制造企业的15个差别(下)
- 中国十大证券app排名 炒股开户安全吗
- [ansible problem processing] remote execution user environment variable loading problem
- Py之toad:toad的简介、安装、使用方法之详细攻略
猜你喜欢

Virtual machines on the same distributed port group but different hosts cannot communicate with each other

Laravel8 uses faker to call factory to fill data

From pair to unordered_ Map, theory +leetcode topic practice

GO语言并发模型-MPG模型

Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers

v-for 中 key的作用和原理

常见的单例模式&简单工厂

Mots clés pour la cartographie es; Ajouter une requête par mot - clé à la requête term; Changer le type de mot - clé de cartographie

Don't underestimate the integral mall. It can play a great role

As a developer, what is the most influential book for you?
随机推荐
Defeat the binary tree!
【ansible问题处理】远程执行用户环境变量加载问题
Bert whitening vector dimension reduction and its application
Mots clés pour la cartographie es; Ajouter une requête par mot - clé à la requête term; Changer le type de mot - clé de cartographie
09_一种比较高效的记忆方法
Unit contour creation method
As a developer, what is the most influential book for you?
One article to get UDP and TCP high-frequency interview questions!
Alibaba OSS object storage service
IList of PostgreSQL
Common sense knowledge points
Redis interview questions
Development of B2B transaction collaborative management platform for kitchen and bathroom electrical appliance industry and optimization of enterprise inventory structure
Restoring to an earlier version in CVS
GO语言-init()函数-包初始化
Laravel 8 realizes auth login
June training (day 24) - segment tree
Keyword of ES mapping; Term query add keyword query; Change mapping keyword type
The difference between V-IF and v-show
CVPR2022 | 可精简域适应